我正在尝试解析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;
答案 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