I am trying to parse an json string in android. String is as-
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);
}
but it gives parsing exception at
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
I am using following imports
import org.json.JSONArray;
import org.json.JSONObject;
Try Using Verbatim String or Pass Escape Character for Double Quotes
response = "\"{\"UserID\":100055,\"VisibleName\":\"sarita\",\"UserSince\":\"September 2015\"}\""
Have a Look at this : http://gmariotti.blogspot.in/2013/04/how-to-work-with-json-in-android.html
just add escape sequence
to your response string as
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();
}
output:
value->﹕ UserID->100055 UserSince->September 2015 VisibleName->sarita
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.