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
spen123
3,53411 gold badges41 silver badges56 bronze badges
1 Answer 1
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
Chris
1,2201 gold badge10 silver badges17 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-java