-1

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.

James Roberts
  • 195
  • 1
  • 4
  • 13
  • 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 '14 at 20:39
  • This might help: http://stackoverflow.com/questions/5245840/how-to-convert-string-to-jsonobject-in-java – RST_7 Feb 20 '14 at 20:42

1 Answers1

0

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");
BVB
  • 5,380
  • 8
  • 41
  • 62