Json.net 反序列化 类 数组的数组
Json.net Deserialize Array of Array of classes
我正在通过 URL 接收 JSON 数据,如下所示:
{
"destination_addresses" : [ "example address" ],
"origin_addresses" : [ "example address" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "220 ft",
"value" : 67
},
"duration" : {
"text" : "1 min",
"value" : 12
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
我正在尝试反序列化为以下 classes:
public class APIDistance
{
public class Rootobject
{
public string[] destination_addresses { get; set; }
public string[] origin_addresses { get; set; }
public Row[] rows { get; set; }
public string status { get; set; }
}
public class Row
{
public Element[] elements { get; set; }
}
public class Element
{
public Distance distance { get; set; }
public Duration duration { get; set; }
public string status { get; set; }
}
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; }
}
}
我的问题是我不知道如何分配 and/or 从 class 读取数据或从 class 读取数据。我遵循了建议您应该使用 JsonConvert.DeserializeObject 然后使用该对象来获取所需的 属性 的示例,但是,我似乎无法找到任何属性:
被告知使用 newtonsoft,因为它“简单”,但没有找到我尝试使用的 Json 结构的任何示例。
Json 的新手,非常感谢任何帮助。提前致谢。
Remove public class APIDistance { which here is just functioning as a
parent for nested classes. Instead deserialize Rootobject like this
JsonConvert.DeserializeObject(json) – Charlieface 18 mins ago
我正在通过 URL 接收 JSON 数据,如下所示:
{
"destination_addresses" : [ "example address" ],
"origin_addresses" : [ "example address" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "220 ft",
"value" : 67
},
"duration" : {
"text" : "1 min",
"value" : 12
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
我正在尝试反序列化为以下 classes:
public class APIDistance
{
public class Rootobject
{
public string[] destination_addresses { get; set; }
public string[] origin_addresses { get; set; }
public Row[] rows { get; set; }
public string status { get; set; }
}
public class Row
{
public Element[] elements { get; set; }
}
public class Element
{
public Distance distance { get; set; }
public Duration duration { get; set; }
public string status { get; set; }
}
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; }
}
}
我的问题是我不知道如何分配 and/or 从 class 读取数据或从 class 读取数据。我遵循了建议您应该使用 JsonConvert.DeserializeObject 然后使用该对象来获取所需的 属性 的示例,但是,我似乎无法找到任何属性:
被告知使用 newtonsoft,因为它“简单”,但没有找到我尝试使用的 Json 结构的任何示例。
Json 的新手,非常感谢任何帮助。提前致谢。
Remove public class APIDistance { which here is just functioning as a parent for nested classes. Instead deserialize Rootobject like this JsonConvert.DeserializeObject(json) – Charlieface 18 mins ago