0

I am parsing JSON data from this link:

http://twyst.in/api/v1/near/30/30

As the data is too large, I am posting only the (problem) relevant data here:

{ "info": "[{\"outlet\":{\"__v\":1,\"_id\":\"5316d59326b019ee59000026\",\"photos\":[],\"twyst_meta\":{\"recommend_list\":[],\"reviews\":[]},\"outlet_meta\":{\"links\":[],\"status\":\"active\",\"accounts\":[\"531574eeae738d654c00000a\",\"535a1a1e827651ca03000122\"]},\"attributes\":{\"dine_in\":true,\"outdoor\":true,\"air_conditioning\":\"Not Available\",\"parking\":\"Available\",\"reservation\":\"Not Required\",\"wifi\":\"Not Available\",\"tags\":[\"Pizza\",\"Pasta\",\"Italian\",\"Galleria\",\"DLF Phase 4\"],\"payment_options\":[\"cash\",\"amex\",\"visa\",\"master\"],\"cuisines\":[\"Italian\",\"Pizza\",\"Gelato\"],\"timings\":\"11 AM - 11 PM\",\"cost_for_two\":{\"min\":3,\"max\":4}},\"links\":{\"other_urls\":[],\"youtube_url\":\"\",\"twitter_url\":\"\",\"facebook_url\":\"https://www.facebook.com/pages/Crusty-gourmet-pizza-more/390843954384130\",\"website_url\":\"\"},\"contact\":{\"emails\":{\"email\":\"[email protected]\",\"person\":\"\"},\"phones\":{\"number\":\"\",\"landline\":\"02355156616\",\"reg_mobile\":[{\"_id\":\"4639320000e4532779b17de7\",\"num\":\"9134000410\"}

This is not the end of JSON data, it goes on...

But when the following line of code is executed, I am getting a JSONException. The e.printStackTrace() method tells me that there is typeMismatch error.

       JSONObject object = new JSONObject(str);        //1
       JSONArray array = object.getJSONArray("info");  //2 

I have tried replacing the line 2 with the following code:

      JSONObject newObject = object.getJSONObject("info");

But here also I encountered the same problem.

I have checked the complete data, it is correct, no syntax error. Can anybody help me with the issue ? Can the presence of double quotes before starting of JSONArray be the root of this problem ?

Thanks.

2
  • I think it will. It will interpret it as a string. Commented May 24, 2014 at 15:53
  • What do you mean ? Presence of double quotes will be a problem, or it will parse, I am not doing it the correct way ? Commented May 24, 2014 at 15:54

2 Answers 2

2

info is not an array or an object. It's a string, which just happens to contain the JSON-encoded representation of another array. You will have to get that string and run it through a second JSON decoding pass:

JSONArray array = new JSONArray(object.getString("info"));
Sign up to request clarification or add additional context in comments.

3 Comments

This is what I was trying to say in the above comment. Mobile is too slow :)
@Rajat The first element of the array is an object, so you can call getJSONObject(0) on the array to get the object, and then use its keys, if that's what you mean.
@Boann: OK I get it. I misunderstood initially.
0
JSONObject json;
Object     info;
JSONArray  infoJsonArray;
JSONObject infoObject;
json = new JSONObject(str);

Object info= json.get("info");
if (info instanceof JSONArray) {
// It's an array
infoJsonArray = (JSONArray)info;
}
else if (info instanceof JSONObject) {
// It's an object
infoObject = (JSONObject)info;
} else {
// It's something else, like a string or number
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.