使用json解析器android java时的JSONexception

时间:2014-11-21 14:27:44

标签: java android json

我无法在我的应用上显示我的搜索结果到webview,因为错误JSONexception,这是我的json:

"items":[
            {
            "kind":"customsearch#result",
            "title":"how  to make android app - Tholabul Ilmi",
            "htmlTitle":"how to make \u003cb\u003eAndroid\u003c/b\u003e app - Tholabul ilmi",
            "link":"http://T*****.com/2014/11/create-android-app.html",
            "displayLink":"T****.com",
            "snippet":"11 Nov 2014 ... You can get completed source like video demo, source code, tutorials on my github ...",
            "cacheId":"OTC6MiDeHu8J",
            "pagemap":
                {
                "metatags":[
                                {
                                    "viewport":"width=device-width, initial-scale=1.0"
                                }
                            ],
                "hcard":[
                                {
                                    "fn":"Tholabul Ilmi"
                                }
                        ]
            }
},

在我的app项目中我有2个文件activity_main.xml(我在那里插入Web视图)和我的java代码。

 private String ParseResult(String json) throws JSONException{
        String parsedResult = "";
   JSONObject jsonObject = new JSONObject(json);
        JSONObject jsonObject_responseData = jsonObject.getJSONObject("items");
        JSONArray jsonArray_results = jsonObject_responseData.getJSONArray("");

        parsedResult += "Google Search APIs (JSON) for : <b>" + search_item + "</b><br/>";
        parsedResult += "Number of results returned = <b>" + jsonArray_results.length() + "</b><br/><br/>";

        for(int i = 0; i < jsonArray_results.length(); i++){

            JSONObject jsonObject_i = jsonArray_results.getJSONObject(i);

            String iTitle = jsonObject_i.getString("title");
            String iContent = jsonObject_i.getString("snippet");
            String iUrl = jsonObject_i.getString("link");

            parsedResult += "<a href='" + iUrl + "'>" + iTitle + "</a><br/>";
            parsedResult += iContent + "<br/><br/>";
        }

        return parsedResult;
    }

我很困惑,我必须填写getJSONArray以及如何使其显示我在myweb博客上搜索的内容,我使用谷歌自定义搜索和在应用程序中当用户键入文本并单击搜索它将显示结果有关它我的博客。我想像谷歌搜索小部件一样创建小部件搜索。

1 个答案:

答案 0 :(得分:0)

看起来,您的JSON字符串无效。

这是有效的字符串...... 尝试使用有效的JSON字符串...

您可以在此网站上查看JSON验证:

http://www.jsoneditoronline.org/

{"items":[
{
"kind":"customsearch#result",
"title":"how  to make android app - Tholabul Ilmi",
"htmlTitle":"how to make \u003cb\u003eAndroid\u003c/b\u003e app - Tholabul ilmi",
"link":"http://T*****.com/2014/11/create-android-app.html",
"displayLink":"T****.com",
"snippet":"11 Nov 2014 ... You can get completed source like video demo, source code, tutorials on my github ...",
"cacheId":"OTC6MiDeHu8J",
"pagemap":{
"metatags":[
{
"viewport":"width=device-width, initial-scale=1.0"
}
],
"hcard":[
{
"fn":"Tholabul Ilmi"
}
]
}
}]}