0

I am having trouble parsing JSON when there is multiple arrays. This is an example of the JSON:

{
   "topics":[
      {
         "posts":[
            {
               "date":"01/01/01 01:01",
               "user":"Example",
               "subject":"Example Topic #1",
               "contents":"Hello!"
            },
            {
               "date":"01/01/01 01:01",
               "user":"Example",
               "subject":"Example Reply #1",
               "contents":"Hello!"
            },
            {
               "date":"01/01/01 01:01",
               "user":"Example",
               "subject":"Example Reply #2",
               "contents":"Hello!"
            }
         ]
      },
      {
         "posts":[
            {
               "date":"01/01/01 01:01",
               "user":"Example",
               "subject":"Example Topic #2",
               "contents":"Hello!"
            },
            {
               "date":"01/01/01 01:01",
               "user":"Example",
               "subject":"Example Reply #1",
               "contents":"Hello!"
            },
            {
               "date":"01/01/01 01:01",
               "user":"Example",
               "subject":"Example Reply #2",
               "contents":"Hello!"
            }
         ]
      }
   ]
}

In each of the posts array the first one is the main topic itself and then the rest are replies, the amount of replies is varied and this is just an example.

What I am looking to do is take the user, subject and contents from the main post, the replies I want to ignore.

What I have tried so far after looking at some tutorials is:

try {
    JSONArray threads = jo.getJSONArray(THREADS_TAG); // jo is a JSONObject parameter.

    for (int i = 0; i < threads.length(); i++) {
        JSONObject c = threads.getJSONObject(i);
        JSONObject post = c.getJSONObject(POSTS_TAG);

        Log.i(TAG, "Name: " + post.getString(NAME_TAG));
    }
    } catch (JSONException e) {
        Log.e(TAG, "Exception:");
        e.printStackTrace();
}

But I am having problems with this and I am getting an exception:

at posts of type org.json.JSONArray cannot be converted to JSONObject

1
  • you need to getJSONObject(0) to obtain the first object of the array Commented Dec 26, 2012 at 17:16

2 Answers 2

3

You can do something like this

try {
    JSONArray threads = jo.getJSONArray("topics");

    for (int i = 0; i < threads.length(); i++) {
        JSONArray posts = threads.getJSONObject(0).getJSONArray("posts");
        user[i]=posts.getJSONObject(0).getString("user");
        subject[i]=posts.getJSONObject(0).getString("subject");
        contents[i]=posts.getJSONObject(0).getString("contents");
    }
    } catch (JSONException e) {
        Log.e(TAG, "Exception:");
        e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

2 Comments

For the part, threads.getJSONArray("posts"), getJSONArray() will only take an integer as the parameter.
@DanielB I am sorry, my mistake. Please see the updated answer.
0

Change for JsonArray parsing as:

  for (int i = 0; i < threads.length(); i++) {

        JSONObject cjsontopic = threads.getJSONObject(i);
        JSONArray jsonarrayposts=cjsontopic.getJSONArray("posts");

        for (int j = 0; j < jsonarrayposts.length(); j++) {
                JSONObject cjsontopic = jsonarrayposts.getJSONObject(j);
        String strpost = cjsontopic.getString(POSTS_TAG);

        String struser = cjsontopic.getString("user");
        String strsubject = cjsontopic.getString("subject");
        String strcontents = cjsontopic.getString("contents");
        //... get other elemets
        Log.i(TAG, "Name: " +strpost);
     }

   }

currently you are trying to get String in JsonObject your Json format is just like JsonObject-->JsonArray-->JsonObject-->JsonArray-->JsonObject-->getString

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.