-2

I am trying to make a program that reads through this JSON text:

{
"Suburban Station Departures: September 1, 2017, 10:18 pm": [
{
  "Northbound": [
    {
      "direction": "N",
      "path": "R7N",
      "train_id": "776",
      "origin": "Trenton",
      "destination": "Chestnut H East",
      "status": "4 min",
      "service_type": "LOCAL",
      "next_station": "30th Street Station",
      "sched_time": "Sep  1 2017 10:26:00:000PM",
      "depart_time": "Sep  1 2017 10:27:00:000PM",
      "track": "2",
      "track_change": null,
      "platform": "B",
      "platform_change": null
    },

The program will retrieve the output from the site, and grabs the last updated portion of text. I've managed to retrieve the site's input, but I'm not sure how I would get the info within the two arrays. I have tried looking at other questions but they are dealing with no arrays/lists at all and just parsing data without lists/arrays.

  • Try to look by yourself first. This post should help you : https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Xavier Bouclet Sep 02 '17 at 03:16

1 Answers1

0

In your data "Suburban Station Departures: September 1, 2017, 10:18 pm" is a String . It should be key pair value according to structure.

According to your JSON Data try this code:

JSONObject obj = new JSONObject("JSON_String");
JSONArray array = obj .getJSONArray("Suburban Station Departures: September 1, 2017, 10:18 pm");
for(int i=0;i<array .length;i++)
{
 JSONObject objDepartures = array .getJSONObject(i);
 JSONArray arrayNorthbound=objDepartures .getJSONArray("Northbound");
   for(int i=0;i<arrayNorthbound.length;i++)
   {
      JSONObject objDepartures = arrayNorthbound.getJSONObject(i);
      String direction=objDepartures .getString("direction");
      // rest same
    }
}
Sumit Kumar
  • 249
  • 1
  • 7