Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Json.NET反序列化复杂对象_C#_Json_Json.net_Deserialization - Fatal编程技术网

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

C# 使用Json.NET反序列化复杂对象,c#,json,json.net,deserialization,C#,Json,Json.net,Deserialization,我需要反序列化从grogle maps 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,

我需要反序列化从grogle maps 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
        }
DataSet data=JsonConvert.DeserializeObject(jsonResponse);
DataTableDataTab=数据表[“元素”];
foreach(dataTab.Rows中的DataRow元素)
{
控制台写入线(元素[“距离]);
//在这里做点别的
}

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

我认为问题在于基于搜索堆栈溢出时发现的类似问题转换为“DataSet”

试试这个,因为我相信它会起作用(您可能需要使用“行”而不是“元素”,但我相信使用JObject的方法将解决“附加文本”的主要问题

 JObject json = JsonConvert.DeserializeObject<JObject>(jsonResponse);
 foreach (Dictionary<string, object> item in data["Elements"])
 {
    foreach (string val in item.Values) {
        Console.WriteLine(val);
    }
  }
JObject json=JsonConvert.DeserializeObject(jsonResponse);
foreach(数据[“元素”]中的字典项)
{
foreach(item.Values中的字符串val){
控制台写入线(val);
}
}

您应该反序列化到与数据匹配的类。您可以在中生成这些类

//使用like
var rootObj=JsonConvert.DeserializeObject(jsonResponse);
foreach(rootObj.rows中的变量行)
{
foreach(row.elements中的var元素)
{
Console.WriteLine(element.distance.text);
}
}
//您可能希望将属性名称更改为.Net约定
//使用[JsonProperty]让序列化程序知道需要的JSON名称
公共类距离
{
公共字符串文本{get;set;}
公共int值{get;set;}
}
公共课时长
{
公共字符串文本{get;set;}
公共int值{get;set;}
}
公共类元素
{
公共距离距离{get;set;}
公共持续时间{get;set;}
公共字符串状态{get;set;}
}
公共类行
{
公共列表元素{get;set;}
}
公共类根对象
{
公共列表目标地址{get;set;}
公共列表源地址{get;set;}
公共列表行{get;set;}
公共字符串状态{get;set;}
}
使用动态:

dynamic json = JsonConvert.DeserializeObject(jsonResponse);

var rowCount = json.rows.Count;
Func<dynamic, int> getElementCount = r => r.elements.Count;
var maxElements = Enumerable.Max(json.rows, getElementCount);

var matrix = new int?[rowCount, maxElements];
for(int i = 0; i < rowCount; i++)
{
    var elements = json.rows[i].elements;
    for(int j = 0; j < elements.Count; j++)
    {
        var element = elements[j];
        matrix[i, j] = element.distance.value;
    }
}
DynamicJSON=JsonConvert.DeserializeObject(jsonResponse);
var rowCount=json.rows.Count;
Func getElementCount=r=>r.elements.Count;
var maxElements=Enumerable.Max(json.rows,getElementCount);
var矩阵=新整数?[rowCount,maxElements];
对于(int i=0;i
谢谢,但是什么是
“数据[“元素”]”?
这是您在上面发布的数据中的JSON对象键名“元素”。哇,我希望我能为json2charp投票两次!;-)
dynamic json = JsonConvert.DeserializeObject(jsonResponse);

var rowCount = json.rows.Count;
Func<dynamic, int> getElementCount = r => r.elements.Count;
var maxElements = Enumerable.Max(json.rows, getElementCount);

var matrix = new int?[rowCount, maxElements];
for(int i = 0; i < rowCount; i++)
{
    var elements = json.rows[i].elements;
    for(int j = 0; j < elements.Count; j++)
    {
        var element = elements[j];
        matrix[i, j] = element.distance.value;
    }
}