Click here to Skip to main content
15,883,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some json for live bus stop arrivals and departures.

{
    "atcocode": "269030083",
    "smscode": "lecdgpja",
    "request_time": "2019-05-25T13:45:14+01:00",
    "name": "St Margaret's Bus Station (Stand SM)",
    "stop_name": "St Margaret's Bus Station",
    "bearing": "",
    "indicator": "Stand SM",
    "locality": "Leicester",
    "location": {
        "type": "Point",
        "coordinates": [
            -1.1339,
            52.6397
        ]
    },
    "departures": {
        "153": [
            {
                "mode": "bus",
                "line": "153",
                "line_name": "153",
                "direction": "Market Bosworth, Terminus (unmarked)",
                "operator": "AMID",
                "date": "2019-05-25",
                "expected_departure_date": null,
                "aimed_departure_time": "13:45",
                "expected_departure_time": null,
                "best_departure_estimate": "13:45",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/AMID/153/outbound/269030083/2019-05-25/13:45/timetable.json?app_id=ID&app_key=KEY",
                "operator_name": "Arriva Midlands"
            },
            {
                "mode": "bus",
                "line": "153",
                "line_name": "153",
                "direction": "Market Bosworth, Terminus (unmarked)",
                "operator": "AMID",
                "date": "2019-05-25",
                "expected_departure_date": null,
                "aimed_departure_time": "14:45",
                "expected_departure_time": null,
                "best_departure_estimate": "14:45",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/AMID/153/outbound/269030083/2019-05-25/14:45/timetable.json?app_id=ID&app_key=KEY",
                "operator_name": "Arriva Midlands"
            },
            {
                "mode": "bus",
                "line": "153",
                "line_name": "153",
                "direction": "Market Bosworth, Terminus (unmarked)",
                "operator": "AMID",
                "date": "2019-05-25",
                "expected_departure_date": null,
                "aimed_departure_time": "15:45",
                "expected_departure_time": null,
                "best_departure_estimate": "15:45",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/AMID/153/outbound/269030083/2019-05-25/15:45/timetable.json?app_id=ID&app_key=KEY",
                "operator_name": "Arriva Midlands"
            }
        ]
    },
    "source": "NextBuses"
}


In this example departures has a member "153" but obviously as the bus names change, the member name will change for example to "N1" etc.

What I have tried:

I have tried making depatures a dictionary but when I foreach through the dictionary I just get null values....

C#
public class Departures
{
    public Dictionary<string, listing_details> listing { get; set; }
}

public class listing_details
{
    public string mode { get; set; }
    public string line { get; set; }
    public string line_name { get; set; }
    public string direction { get; set; }
    public string @operator { get; set; }
    public string date { get; set; }
    public string expected_departure_date { get; set; }
    public string aimed_departure_time { get; set; }
    public string expected_departure_time { get; set; }
    public string best_departure_estimate { get; set; }
    public string source { get; set; }
    public string dir { get; set; }
    public string id { get; set; }
    public string operator_name { get; set; }
}


Can someone suggest how I would iterate through different name bus lists please? In this example it will be departures.153 but for the N1 bus but it might be departures.N1 . Sadly I won't know the bus names until I get the JSON so I am competely perplexed. Any help would be appreciated! Thanks.
Posted
Updated 29-May-19 6:11am
v4
Comments
F-ES Sitecore 25-May-19 9:34am    
It's impossible to give specific help if we don't know what you're using to deserialise the json. I believe newtonsoft's json.net can cope with dynamic json.
jerry0davis 25-May-19 9:39am    
As you predited - NewtonsoftJSON

BusTimeTable resultsBTT = JsonConvert.DeserializeObject<BusTimeTable>(responseString);


How do I itterate through dynamic JSON using NewtonsoftJSON?

Thanks

Might need to play around with this a bit but this is the basics

JObject o = JObject.Parse(responseString);

// one way to read the values of the main object
string atcocode = o.Value<string>("atcocode");

JToken departures = o["departures"];

// your departures node only has one property (eg "153") so get it using First
JToken departure = departures.First;

JProperty prop = ((JProperty)departure);
string number = prop.Name; // 153

// get the items in 153 as a list
List<listing_details> details = prop.Value.ToObject<List<listing_details>>(); 
 
Share this answer
 
What you can do is write a custom converter and shape the results into a format that you can work with. I discuss working with custom converters in details here:
Working with JSON in C# & VB > Non-Standard Types and Data Structure Types[^]
 
Share this answer
 
There are multiple listings for each bus. The simplest solution would be:
C#
public class Departures
{
    public Dictionary<string, List<listing_details>> departures { get; set; }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900