0

我有那个 JSON 响应

           {"as_of":"2013-04-22T19:50:41Z","trends":[{"events":null,
            "query":"%23RhymeATweepsName","url":"http:\/\/twitter.com\/search?
            q=%23RhymeATweepsName","promoted_content":null,
            "name":"#RhymeATweepsName"},                
            {"events":null,"query":"%23EarthDayPK","url":
            "http:\/\/twitter.com\/search?
            =%23EarthDayPK","promoted_content":null,"name":
            "#EarthDayPK"}],"locations":
            [{"woeid":*******,"name":"********"}],"created_at":"2013-04-22T19:38:16Z"}

我正在用以下代码解析它

             jArray = new JSONArray(result);
                JSONObject post = null;
                for (int ii = 0; ii < jArray.length(); ii++) {
                    post = jArray.getJSONObject(ii);
                     String name = post.getJSONObject("trends").getString("name") + "\n";
                }
            }

但它抛出异常“JSONArray 无法转换为 JSONObject 错误”

4

1 回答 1

4

如您的 JSON 所示,trends不是 a JSONObject,而是 a JSONArray

"trends":[
    {
        "events":null,
        "query":"%23RhymeATweepsName",
        "url":"http:\/\/twitter.com\/search? q=%23RhymeATweepsName",
        "promoted_content":null,
        "name":"#RhymeATweepsName"
    },
    {
        "events":null,
        "query":"%23EarthDayPK",
        "url":"http:\/\/twitter.com\/search? =%23EarthDayPK",
        "promoted_content":null,
        "name":"#EarthDayPK"
    }
]

你应该像这样解析它:

String name = post.getJSONArray("trends").getJSONObject(0).getString("name");
//                                        or iterate... ^^
于 2013-04-22T19:58:39.337 回答