wow
wow

Reputation: 3899

Parsing JSON with mutiple JSON Arrays

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

Upvotes: 0

Views: 320

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

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

Upvotes: 0

Antrromet
Antrromet

Reputation: 15414

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();
}

Upvotes: 3

Related Questions