简体   繁体   English

C#RestSharp-反序列化非标准JSON

[英]C# RestSharp - Deserialize non standard JSON

I am trying to use RestSharp to Deserialize a JSON string. 我正在尝试使用RestSharp反序列化JSON字符串。 However I am getting caught on the slightly non standard structure around the "555111222" part. 但是,我被“ 555111222”部分周围的稍微非标准的结构所吸引。

The number 555111222 is a device serial number so it can change or there can be multiple serial numbers in the JSON response. 号码555111222是设备序列号,因此它可以更改,或者JSON响应中可以有多个序列号。

{
"requestid": "42",
"data": {
    "555111222": {
        "gps_data": [
            {
                "longitude": -73.952284,
                "latitude": 40.755988,
                "time": "2016-06-30 09:41:21"
            },
            {
                "longitude": -73.952284,
                "latitude": 40.755988,
                "time": "2016-06-30 09:41:22"
            },
            {
                "longitude": -73.952284,
                "latitude": 40.755988,
                "time": "2016-06-30 09:41:23"
            }
        ]
    }
  }
}

Json2csharp.com gives me the following Json2csharp.com给了我以下内容

public class GpsData
{
    public double longitude { get; set; }
    public double latitude { get; set; }
    public string time { get; set; }
}

public class __invalid_type__555111222
{
    public List<GpsData> gps_data { get; set; }
}

public class Data
{
    public __invalid_type__555111222 __invalid_name__555111222 { get; set; }
}

public class RootObject
{
    public string requestid { get; set; }
    public Data data { get; set; }
}

Im not sure how to handle the invalid_type errors in my class. 我不确定如何处理班级中的invalid_type错误。

Thank you 谢谢

It looks to me like your Data property should be Dictionary<string, GpsDataWrapper> where GpsDataWrapper is just a class with a GpsData property: 在我看来,您的Data属性应该是Dictionary<string, GpsDataWrapper> ,其中GpsDataWrapper只是具有GpsData属性的类:

public class GpsData
{
    public double longitude { get; set; }
    public double latitude { get; set; }
    public string time { get; set; }
}

public class GpsDataWrapper
{
    public List<GpsData> gps_data { get; set; }
}

public class RootObject
{
    public string requestid { get; set; }
    public Dictionary<string, GpsDataWrapper> data { get; set; }
}

I'd personally then convert all of these to use .NET naming conventions and apply attributes to specify the names used in JSON. 然后,我个人将所有这些转换为使用.NET命名约定,并应用属性来指定JSON中使用的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM