当有多个数组时,我无法解析 JSON。这是 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!"
}
]
}
]
}
在每个posts
数组中,第一个是主题本身,然后是回复,回复的数量各不相同,这只是一个示例。
我要做的是从user
主帖中获取我想忽略的回复。subject
contents
在查看了一些教程后,我到目前为止所尝试的是:
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();
}
但是我遇到了这个问题,我遇到了一个例外:
org.json.JSONArray 类型的帖子无法转换为 JSONObject