具有不同成员名称的JSON反序列化类 [英] JSON deserialization class with varying member names

查看:106
本文介绍了具有不同成员名称的JSON反序列化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些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.

推荐答案

可能需要稍微玩一下但这是基础知识



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>>(); 


您可以做的是编写自定义转换器并将结果整形为可以使用的格式。我在这里详细讨论使用自定义转换器:

在C#中使用JSON& VB>非标准类型和数据结构类型 [ ^ ]
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[^]


每个总线有多个列表。最简单的解决方案是:

There are multiple listings for each bus. The simplest solution would be:
public class Departures
{
    public Dictionary<string, List<listing_details>> departures { get; set; }
}


这篇关于具有不同成员名称的JSON反序列化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆