複数の配列がある場合、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 に変換できません