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