解析JSON响应以从HTML获取正文

时间:2014-06-13 09:58:10

标签: javascript android json webview

我收到了以下json回复。如何从中获取身体内容,以便我可以检查下面的json条件:

{
    "googleplus": {
        "error_message": "User did not grant permission + access_denied",
        "error_code": 2020
    }
}

我尝试使用以下语句来获取html的主体,但它似乎与if语句

中的条件不匹配
mWebView.loadUrl("javascript:window.HTMLOUT.showHTML(document.getElementsByTagName('body')[0].innerHTML);");


class MyJavaScriptInterface   
{ 
    @JavascriptInterface
    public void showHTML(String html)  
    { 

        if(html.contains(some string from body of html))
        {
            //do something
        } 
    }

}

我想要的只是我在if条件中使用的字符串应该是什么字符串才能成为真实

1 个答案:

答案 0 :(得分:0)

因为你的json是给定的

{
  "googleplus": {
    "error_message": "User did not grant permission + access_denied",
    "error_code": 2020
  }
}

解析json数据如下

JSONObject primaryObject=new JSONObject(//pass yours json string here);
        JSONObject googleplus=primaryObject.getJSONObject("googleplus");

        String error_message=googleplus.getString("error_message");//use it if you need
        String error_code=googleplus.getString("error_code");