0

I have json file like this

[
     {

       "topic": "Example1", 
       "ref": {
            "1": "Example Topic", 
            "2": "Topic"
        }, 
       "contact": [
            {
                "ref": [
                    1
                ], 
                "corresponding": true, 
                "name": "XYZ"
            }, 
            {
                "ref": [
                    1
                ], 
                "name": "ZXY"
            }, 
            {
                "ref": [
                    1
                ], 
                "name": "ABC"
            }, 
            {
                "ref": [
                    1, 
                    2
                ], 
                "name":"BCA"
            }
        ] , 

        "type": "Presentation"
     }
]

I want to parse the ref array. I tried this. But showing error.

jsonArray.getJSONObject(index).getJSONArray("ref").getJSONObject(index).toString()

Now my question is

1) What is the correct way to parse that array's content.

4
  • 1
    There are many ref in this JSON; Which one do you want? Commented Jul 17, 2013 at 20:51
  • @JBNizet"ref": { "1": "Example Topic", "2": "Topic" } Commented Jul 17, 2013 at 20:53
  • When you're just learning, especially, do not "daisy-chain" operations together like that. Make each method call a separate statement and assign the result to a temp (which is then used in the next statement). This makes it much easier to debug and easier to edit when you discover you need to rearrange things. Commented Jul 17, 2013 at 21:31
  • And go to json.org to see the (very simple) JSON syntax spec. It takes all of 5 minutes to learn, and knowing it makes processing JSON much easier. Commented Jul 17, 2013 at 21:33

2 Answers 2

3

The whole JSON is an array (starts with [). Its first element is an object (starts with {). This object has an attribute "ref". Its value is an object (starts with {).

So, to get this object, you need

jsonArray.getJSONObject(index).getJSONObject("ref")
Sign up to request clarification or add additional context in comments.

Comments

1

I solved it By

 JSONObject arJS = jsonArray.getJSONObject(index).getJSONObject("ref");
 for(int counter = 1 ; jo<=jsonArray.getJSONObject(index).getJSONObject("ref").length();counter++){

               String value = arJS.getString(String.valueOf(counter));

           }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.