简体   繁体   English

转换“ JSONArray无法转换为JSONObject错误”时发生异常

[英]Exception while converting “JSONArray cannot be converted to JSONObject error”

I have that JSON Response 我有那个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"}

and i am parsing it with the following code 我用下面的代码解析

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

But it throwing Exception "JSONArray cannot be converted to JSONObject error" 但是它抛出异常“ JSONArray无法转换为JSONObject错误”

As seen in your JSON, trends is not a JSONObject , but a JSONArray : 从JSON中可以看出, trends不是JSONObject ,而是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"
    }
]

You should parse it pretty much like this: 您应该像这样解析它:

String name = post.getJSONArray("trends").getJSONObject(0).getString("name");
//                                        or iterate... ^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM