c# - 使用 Json.NET 反序列化复杂对象

标签 c# json json.net deserialization

我需要反序列化从 grogle map api 返回的这个 json:

{
    "destination_addresses": [
        "Via Medaglie D'Oro, 10, 47121 Forlì FC, Italia",
        "Via Torino, 20123 Milano, Italia",
        "Via Guglielmo Marconi, 71, 40121 Bologna, Italia",
        "Via Irnerio, 40126 Bologna, Italia"
    ],
    "origin_addresses": [
        "Via Medaglie D'Oro, 10, 47121 Forlì FC, Italia",
        "Via Torino, 20123 Milano, Italia",
        "Via Guglielmo Marconi, 71, 40121 Bologna, Italia",
        "Via Irnerio, 40126 Bologna, Italia"
    ],
    "rows": [
        {
            "elements": [
                {
                    "distance": {
                        "text": "1 m",
                        "value": 0
                    },
                    "duration": {
                        "text": "1 min",
                        "value": 0
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "286 km",
                        "value": 286281
                    },
                    "duration": {
                        "text": "2 ore 48 min",
                        "value": 10083
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "80,1 km",
                        "value": 80088
                    },
                    "duration": {
                        "text": "1 ora 3 min",
                        "value": 3789
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "77,6 km",
                        "value": 77594
                    },
                    "duration": {
                        "text": "57 min",
                        "value": 3422
                    },
                    "status": "OK"
                }
            ]
        },
        {
            "elements": [
                {
                    "distance": {
                        "text": "288 km",
                        "value": 287811
                    },
                    "duration": {
                        "text": "2 ore 48 min",
                        "value": 10052
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "1 m",
                        "value": 0
                    },
                    "duration": {
                        "text": "1 min",
                        "value": 0
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "212 km",
                        "value": 212423
                    },
                    "duration": {
                        "text": "2 ore 8 min",
                        "value": 7664
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "218 km",
                        "value": 218219
                    },
                    "duration": {
                        "text": "2 ore 9 min",
                        "value": 7740
                    },
                    "status": "OK"
                }
            ]
        },
        {
            "elements": [
                {
                    "distance": {
                        "text": "78,5 km",
                        "value": 78528
                    },
                    "duration": {
                        "text": "56 min",
                        "value": 3346
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "212 km",
                        "value": 212190
                    },
                    "duration": {
                        "text": "2 ore 5 min",
                        "value": 7519
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "1 m",
                        "value": 0
                    },
                    "duration": {
                        "text": "1 min",
                        "value": 0
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "2,0 km",
                        "value": 1979
                    },
                    "duration": {
                        "text": "5 min",
                        "value": 316
                    },
                    "status": "OK"
                }
            ]
        },
        {
            "elements": [
                {
                    "distance": {
                        "text": "74,7 km",
                        "value": 74719
                    },
                    "duration": {
                        "text": "55 min",
                        "value": 3278
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "218 km",
                        "value": 217951
                    },
                    "duration": {
                        "text": "2 ore 9 min",
                        "value": 7712
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "3,8 km",
                        "value": 3782
                    },
                    "duration": {
                        "text": "11 min",
                        "value": 671
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "1 m",
                        "value": 0
                    },
                    "duration": {
                        "text": "1 min",
                        "value": 0
                    },
                    "status": "OK"
                }
            ]
        }
    ],
    "status": "OK"
}

我需要创建一个距离矩阵,因此我只对“距离”内的“值”字段感兴趣。

我尝试过这种方法:

DataSet data = JsonConvert.DeserializeObject<DataSet>(jsonResponse);
        DataTable dataTab = data.Tables["Elements"];
        foreach (DataRow elements in dataTab.Rows)
        {
            Console.WriteLine(elements["distance"]);
            //Do something else here
        }

但是 JsonConvert 返回“完成反序列化对象后在 JSON 字符串中找到的其他文本。”

最佳答案

您应该反序列化为与您的数据匹配的类。您可以在 http://json2csharp.com/ 生成这些类.

// use like
var rootObj = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
foreach (var row in rootObj.rows)
{
    foreach (var element in row.elements)
    {
        Console.WriteLine(element.distance.text);
    }
}

// you might want to change the property names to .Net conventions
// use [JsonProperty] to let the serializer know the JSON names where needed
public class Distance
{
    public string text { get; set; }
    public int value { get; set; }
}

public class Duration
{
    public string text { get; set; }
    public int value { get; set; }
}

public class Element
{
    public Distance distance { get; set; }
    public Duration duration { get; set; }
    public string status { get; set; }
}

public class Row
{
    public List<Element> elements { get; set; }
}

public class RootObject
{
    public List<string> destination_addresses { get; set; }
    public List<string> origin_addresses { get; set; }
    public List<Row> rows { get; set; }
    public string status { get; set; }
}

关于c# - 使用 Json.NET 反序列化复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20568448/

相关文章:

json - Postgres - 将两个数组合并成一个 JSON

c# - 如何在json单引号中编写c#字符串

c# - 使用 Moq 模拟使用可选参数的方法

c# - 如何将隐藏字段值从 View 传递到 Controller ASP.NET MVC 5?

c# - 如何将我的 asp.net 项目发布到本地 iis?

json - 无法生成 JSON - javax.ws.rs.WebApplicationException : com. sun.jersey.api.MessageException

c# - Parallel.Foreach 在幕后做了什么?

jquery - JSONP:如何构造ajax查询?

c# - 如何将任何 pascal 大小写 JSON 对象转换为驼峰大小写 JSON 对象?

c# - 如何使只读结构 XML 可序列化?