问题描述
我有一些json用于现场巴士到达和离开。
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"
}
在此示例中,离开的成员为153但显然随着总线名称的更改,成员名称将更改为例如N1等。
我尝试过:
我尝试过将字典设为字典,但是当我通过字典进行操作时,我只得到空值....
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....
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; }
}
有人可以建议我如何迭代不同的名字总线列表吗?在这个例子中它将离开.153但是对于N1总线但它可能是离开.N1。可悲的是,在获得JSON之前我不会知道总线名称所以我很困惑。任何帮助,将不胜感激!谢谢。
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.
推荐答案
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>>();
public class Departures
{
public Dictionary<string, List<listing_details>> departures { get; set; }
}
这篇关于具有不同成员名称的JSON反序列化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!