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数组中,第一个是主题本身,然后是回复,回复的数量各不相同,这只是一个示例。

我要做的是从user主帖中获取我想忽略的回复。subjectcontents

在查看了一些教程后,我到目前为止所尝试的是:

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 中获取字符串,您的 Json 格式就像JsonObject-->JsonArray-->JsonObject-->JsonArray-->JsonObject-->getString

于 2012-12-26T17:13:39.057 回答