JenuRudan JenuRudan - 6 months ago 8
JSON Question

Unable to parse to JSONARRAY

I get JSON response in my android application in which it contains an array which i parse to JSON in my code, it can't parse and the JSON looks fine

Code

JSONArray A=response.getJSONArray("Favs");


Server Response

{"Success":true,"FavFound":true,"Favs":"[{\"ID\":1,\"UserI\":1,\"LocName\":\"A7a\",\"Lat\":5.0,\"Lng\":5.0}]"}


Error

org.json.JSONException: Value [{"ID":1,"UserID":1,"LocName":"A7a","Lat":5.0,"Lng":5.0}] at Favs of type java.lang.String cannot be converted to JSONArray

JLT JLT
Answer

The value of "Favs" is a string. Because it is wrapped with quotations ""

Look carefully:

"Favs":"[{\"ID\":1,\"UserI\":1,\"LocName\":\"A7a\",\"Lat\":5.0,\"Lng\":5.0}]"

If it is a JSON Array it should look like this:

"Favs":[{"ID":1,"UserI":1,"LocName":"A7a","Lat":5.0,"Lng":5.0}]

with no quotations outside the brackets and no slash notations before those quotations inside too.