Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have this string which ive validated as JSON:

{"listings":[{"Listing_ID":"1","Event_ID":"1","Venue_ID":"1","Start_Date":"19\/11\/2013","End_Date":"04\/01\/2014","Frequency":"Every Day"},{"Listing_ID":"2","Event_ID":"1","Venue_ID":"4","Start_Date":"20\/01\/2014","End_Date":"21\/01\/2014","Frequency":"Every 2 Da"},{"Listing_ID":"3","Event_ID":"3","Venue_ID":"4","Start_Date":"22\/01\/2014","End_Date":"23\/01\/2014","Frequency":"Every Day"}]}

And i wish to convert it into a JSON array. Ive read around and believe i first have to turn it into an object but im struggling to find a solution.

Any help would be much appreciated.

share|improve this question
    
there's no such thing as "json array". There's json strings, which contain encoded data structures... What you have there is a json string which contains an object containing an array of objects... if it gets decoded. –  Marc B Feb 20 at 20:39
    
This might help: stackoverflow.com/questions/5245840/… –  RST_7 Feb 20 at 20:42
add comment

marked as duplicate by Brian Roach, njzk2, Selvin, laalto, Siddharth Lele Feb 22 at 5:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

You should be able to use the JSONArray object for this. http://developer.android.com/reference/org/json/JSONArray.html Try using it with the JSONTokener. http://developer.android.com/reference/org/json/JSONTokener.html

Here's an example:

String json = "{\"listings\":[{\"Listing_ID\":\"1\",\"Event_ID\":\"1\",\"Venue_ID\":\"1\",\"Start_Date\":\"19\/11\/2013\",\"End_Date\":\"04\/01\/2014\",\"Frequency\":\"Every Day\"},{\"Listing_ID\":\"2\",\"Event_ID\":\"1\",\"Venue_ID\":\"4\",\"Start_Date\":\"20\/01\/2014\",\"End_Date\":\"21\/01\/2014\",\"Frequency\":\"Every 2 Da\"},{\"Listing_ID\":\"3\",\"Event_ID\":\"3\",\"Venue_ID\":\"4\",\"Start_Date\":\"22\/01\/2014\",\"End_Date\":\"23\/01\/2014\",\"Frequency\":\"Every Day\"}]}";
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
JSONArray locations = object.getJSONArray("listings");
share|improve this answer
add comment

You can check this thread: How to convert String to JSONObject in Java

Using org.json library:

JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");

And some libraries to do that: •org.json •org.json.me •Jackson JSON Processor •Json-lib •JSON Tools •json-simple •Stringtree •SOJO •Restlet •Jettison •json-taglib •XStream •JsonMarshaller •Flexjson •JON tools •google-gson •Argo •Pivot •jsonij •fastjson

share|improve this answer
    
Copying and pasting someone else's answer from a different SO Q is really quite rude. –  Brian Roach Feb 20 at 21:17
    
I added the reference link in How to convert String to json. –  MrMins Feb 20 at 21:19
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.