0

複数の配列がある場合、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配列では、最初のトピック自体がメイン トピックであり、残りは返信です。返信の量はさまざまで、これは単なる例です。

私がやろうとしているのはusersubjectメイン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 に変換できません

4

2 に答える 2

3

このようなことができます

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();
}
于 2012-12-26T17:13:54.490 に答える
0

JsonArray の解析を次のように変更します。

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

   }

現在、JsonObject で String を取得しようとしています。Json 形式は次のようになります。JsonObject-->JsonArray-->JsonObject-->JsonArray-->JsonObject-->getString

于 2012-12-26T17:13:39.057 に答える