在android中解析整数值中没有双四边形的json字符串

时间:2015-09-19 05:53:07

标签: java android json

我正在尝试解析android中的json字符串。字符串为 -

   String response = "[{"UserID":100055,"VisibleName":"sarita","UserSince":"September 2015"}]";
    JSONArray jArray = new JSONArray(response);
    for(int i = 0 ; i <jArray.length();i++){

        JSONObject jObject = jArray.getJSONObject(i);
    }

但它在

处提供了解析异常
 JSONArray jArray = new JSONArray(response);

org.json.JSONException: Value [{"UserID":100054,"VisibleName":"Sarita","UserSince":"September 2015"}] of type java.lang.String cannot be converted to JSONObject

我正在使用以下导入

  

import org.json.JSONArray;

     

import org.json.JSONObject;

2 个答案:

答案 0 :(得分:0)

尝试使用逐字字符串或双引号传递转义字符

 response = "\"{\"UserID\":100055,\"VisibleName\":\"sarita\",\"UserSince\":\"September 2015\"}\""

看看这个:http://gmariotti.blogspot.in/2013/04/how-to-work-with-json-in-android.html

答案 1 :(得分:0)

只需将escape sequence添加到您的回复字符串

即可
      String response = "[{\"UserID\":100055,\"VisibleName\":\"sarita\",\"UserSince\":\"September 2015\"}]";
        JSONArray jArray = null;
        try {
            jArray = new JSONArray(response);
            for(int i = 0 ; i <jArray.length();i++){
                JSONObject jObject = jArray.getJSONObject(i);
                Log.e("value->","UserID->"+jObject.getString("UserID")+" UserSince->"+jObject.getString("UserSince")+" VisibleName->"+jObject.getString("VisibleName"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

输出:

value->﹕ UserID->100055 UserSince->September 2015 VisibleName->sarita