无法正确解析JSON

时间:2015-03-07 06:43:10

标签: java android json mobile

我在解析Android中的JSON响应时遇到了一些问题。我得到的回应是:

{

"response": "{\"session_token\":\"48500d8e42acc09aa45cb8f3a7ba2b30\",\"user_login\":\"newoff2\",\"user_id\":\"62\",\"user_profile_img\":\"http://onepgr.com/system/photos/62/medium/userfile054c35e29.png?1422089771\",\"success\":\"0\",\"user_email\":\"newoff2@pdmoffice.com\"}"

}

我需要user_loginsuccessuser_profile_imguser_email的值。这是我到目前为止所尝试的内容,但它不能满足我的需求:

HttpResponse response = httpClient.execute(httpPost);
// write response to log
Log.d("Http Post Response:", response.toString());
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();

Log.d("Final Response",json);


jsonObject = new JSONObject(json);
JSONObject json1=jsonObject.getJSONObject("response");
String str = json1.getString("success");
Log.e("Parsed data is",str);

4 个答案:

答案 0 :(得分:1)

使用Scanner删除\

String resultStr = new Scanner(json).useDelimiter("\\A").next();
jsonObject = new JSONObject(resultStr);

Above用于BufferedInputStream获取JSON字符串。


[<强>更新

对于BufferReader,需要使用StringBuilder来获取JSON字符串:

StringBuilder strBuilder = new StringBuilder();

 String line;
 while ((line = reader.readLine()) != null) {
    strBuilder.append(line);
}

//for your JSON string, should use 'JSONTokener' to parse
jsonObject = (JSONObject) new JSONTokener(strBuilder.toString()).nextValue();
JSONObject json1=jsonObject.getJSONObject("response");
String str = json1.getString("success");

这适用于您的情况!

答案 1 :(得分:1)

使用此

    json=json.replace("\\\"", "\"");
    Log.e("resule",json);
    try {
        JSONObject jsonObject = new JSONObject(json);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  

答案 2 :(得分:1)

您可以使用正则表达式使您的字符串JSON可解析

var res = data.replace(/\\"/g, '').replace(/\"{/g, '{').replace(/\}"/g, '}');
var jsonData = JSON.parse(res);
alert(jsonData.response.user_login);

这是FIDDLE

注意:在小提琴中,我已经用''声明你的JSON使其成为完整的字符串

答案 3 :(得分:1)

Try this....

InputStream inputStream = null;
    String result = null;
    try {
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();

        Log.d("Result",result);

        JSONObject jsonObject = new JSONObject(result);
            String resJson=jsonObject.getString("response");
        Log.d("Result",resJson);
        JSONObject jsparam=new JSONObject(resJson);
        String success=jsparam.getString("success");

        Log.d("Value for success",success);
        // JSONObject json1=jsonObject.getJSONObject("response");
        //String objResponse = json1.getString("success");
    } catch (Exception e) {
        // Oops
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }