-1

How do i parse a JSON that is like this?

[{
    "id" : 28010942,
    "type" : "trafficlight",
    "title" : "225 - Shaw Blvd. / Gomezville - Flashing",
    "activeFrom" : "Apr 30, 2013 6:18:00 PM",
    "publiclyVisible" : true,
    "locationLat" : 14.589366803498653,
    "locationLon" : 121.03713870048522,
    "publicDescription" : ""
}, {
    "id" : 28010939,
    "type" : "trafficlight",
    "title" : "301 - Andalucia (A. Mendoza) / P. Campa - No Display",
    "activeFrom" : "Apr 30, 2013 6:00:00 PM",
    "publiclyVisible" : true,
    "locationLat" : 14.608034456366056,
    "locationLon" : 120.98599433898926,
    "publicDescription" : ""
} ...
]

I can parse ones that have an object and jsonArrays but this one has neither. How can i iterate through these and be able to store each information like the "id". I'm using the org.json library, or should i use a different one?

linus
  • 481
  • 5
  • 18

1 Answers1

1

If you need to parse try the below

[           // json array node
    {       // json object node
        "id": 28010942,      
        "type": "trafficlight",

To parse

   JSONArray jarray = new JSONArray("Myjsonstring");
   for(int i=0 ;i<jarray.length();i++)
   {
   JSONObject jb = (JSONObject)jarray.get(i); 
   String id = jb.getString("id");
   String type = jb.getString("type");
   // same for others title, activeform..
   }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 1
    Thanks! I didn't think to create a jsonArray using the string immediately. Thank you very much! – linus Aug 30 '13 at 16:11