【问题标题】:Can't properly parse JSON无法正确解析 JSON
【发布时间】:2015-03-07 06:43:10
【问题描述】:

我在 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);

【问题讨论】:

  • 您的 json 响应格式错误
  • 我知道兄弟,,但是如何解码它.. 导致它来自客户端并且无法更改@Ravi
  • 只需将 \" 替换为 "
  • 应该是这样的 { "response": {"session_token":"48500d8e42acc09aa45cb8f3a7ba2b30","user_login":"newoff2","user_id":"62","user_profile_img":"@ 987654321@"} }
  • yaa Ravi 但是 .. 我只有 api 没有来自客户端的任何其他东西

标签: java android json mobile


【解决方案1】:

使用这个

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

【讨论】:

  • 谢谢 .. 让我试试
  • 字符 13 处的预期文字值
  • E/resule:{"response":{\"session_token\":\"835eb24945d16aec029b90b6012f0059\",\"user_login\":\"newoff2\",\"user_id\":\" 62\",\"user_profile_img\":\"onepgr.com/system/photos/62/medium/…\",\"成功\":\"0\",\"user_email\":\"newoff2@pdmoffice.com\"}}
  • W/System.err: org.json.JSONException: {"response":{\"session_token\":\"835eb24945d16aec029b90b6012f0059\",\"user_login\" 的字符 13 处的预期文字值:\"newoff2\",\"user_id\":\"62\",\"user_profile_img\":\"onepgr.com/system/photos/62/medium/…\",\"成功\":\"0\",\"user_email\" :\"newoff2@pdmoffice.com\"}}
  • 第 16 个字符处的未终止对象
【解决方案2】:

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

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

这里是FIDDLE

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

【讨论】:

    【解决方案3】:

    使用Scanner 删除\

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

    以上用于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");
    

    这应该适用于您的情况!

    【讨论】:

    • 感谢回复..让我试试这个.. :)
    • 响应:{"response":"{\"session_token\":\"09b210402c75b2900c77b5b5e486d836\",\"user_login\":\"newoff2\",\"user_id\":\"62 \",\"user_profile_img\":\"onepgr.com/system/photos/62/medium/…\",\"success\":\"0\",\"user_email\":\"newoff2@pdmoffice.com\"}"}
    • W/System.err: org.json.JSONException: Value {"session_token":"09b210402c75b2900c77b5b5e486d836","user_login":"newoff2","user_id":"62","user_profile_img": “onepgr.com/system/photos/62/medium/…”} 在 java.lang.String 类型的响应中无法转换为 JSONObject System.err:在 org.json.JSON.typeMismatch(JSON.java:100)
    【解决方案4】:
    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){}
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-02
      • 2020-07-24
      • 2013-10-14
      • 2012-08-12
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多