2

I have a string that looks like this

"{"resturant_name": "Chipotle", "street": "431 Liberty St"},
{"resturant_name": "MCDoNalds", "street": "1 Main St"},
{"resturant_name": "Wednys", "street": "5 Main St"}"

And I want to turn into a JSONArray so I can loop though and get the name?

But when I do

JSONArray jsonArray = new JSONArray(string);

I get an error

type of org.json.JSONObject cannot be converted to JSONArray,

How can I make this a JSONArray so I can loop though it?

Thanks

asked Sep 1, 2015 at 0:59

1 Answer 1

4

That's because your string is not JSON. It's close, but not quite. JSON standards dictate that the structure should either be an Object or an Array. To create an Array, it must begin with "[" and end with "]". So, your string should look like:

[{"resturant_name": "Chipotle", "street": "431 Liberty St"},
{"resturant_name": "MCDoNalds", "street": "1 Main St"},
{"resturant_name": "Wednys", "street": "5 Main St"}]
answered Sep 1, 2015 at 1:05
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.