C# 如何使用JSON解析来自google maps API的响应?
我想用谷歌地图距离矩阵API计算C#中点之间的距离 我使用以下代码发出请求:C# 如何使用JSON解析来自google maps API的响应?,c#,json,google-maps,C#,Json,Google Maps,我想用谷歌地图距离矩阵API计算C#中点之间的距离 我使用以下代码发出请求: private void MapsAPICall() { //Pass request to google api with orgin and destination details HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://maps.googleapis.com
private void MapsAPICall()
{
//Pass request to google api with orgin and destination details
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("http://maps.googleapis.com/maps/api/distancematrix/json?origins="
+ "51.123959,3.326682" + "&destinations=" + "51.158089,4.145267"
+ "&mode=Car&language=us-en&sensor=false");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
if (!string.IsNullOrEmpty(result))
{
Distance t = JsonConvert.DeserializeObject<Distance>(result);
}
}
}
下面是我收到的json响应示例:
如何将距离和持续时间解析到距离类中?这是我第一次使用Json,所以我对它没有经验
Ps:我已经安装了json.net库。需要使用“距离”类型对json模式进行正确建模,例如:
public string[] destination_addresses { get; set; }
public string[] origin_addresses { get; set; }
public Row[] rows { get; set; }
public string status { get; set; }
(当然,像Row
这样的子类型也需要建模)
除此之外,您对Json.Net的使用:
Distance t = JsonConvert.DeserializeObject<Distance>(result);
距离t=JsonConvert.DeserializeObject(结果);
应该可以可以选择“将JSON粘贴为类”。这将生成反序列化JSON所需的类
使用此选项将为您生成以下内容:
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 Element[] elements { get; set; }
}
public class Parent
{
public string[] destination_addresses { get; set; }
public string[] origin_addresses { get; set; }
public Row[] rows { get; set; }
public string status { get; set; }
}
(如果需要,可以重构生成的代码以使其更具可读性。您需要添加Json.NET属性以确保序列化仍然有效)
然后在代码中,可以使用以下行对代码进行反序列化:
Parent result = JsonConvert.DeserializeObject<Parent>(json);
Parent result=JsonConvert.DeserializeObject(json);
Visual Studio 2015+的更新
作为Wouter de Kort的补充,这里是Visual Studio 2015+的更新
Visual Studio 2015+提供了一个方便的“将JSON粘贴为类”的功能,这一功能更值得一提
要自行生成GoogleMaps API JSON反序列化所需的类:
https://maps.googleapis.com/maps/api/distancematrix/json?origins=30.3449153,-81.860543&目的地=33.7676932,-84.4906437&语言=en-US&单位=imperial&key=
Ctrl+C
)生成的原始JSON响应从浏览器/客户端复制到剪贴板.cs
文件.cs
文件中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; }
}
从那时起,你可以听从沃特的其余建议
Rootobject result = JsonConvert.DeserializeObject<Rootobject>(json);
Rootobject result=JsonConvert.DeserializeObject(json);
虽然很晚了,但可能对某些人有用。在这种情况下,我们有一个JSON字符串,并且希望有一个正确的类结构来正确地反序列化它,您可以使用在线工具
只需粘贴JSON字符串并单击Generate按钮。它将为您提供所需的类结构。就这么简单。当您希望避免安装外部工具时,此方法非常有用 对不起,我正在编辑中-希望最新的编辑能更全面地回答这个问题
Rootobject result = JsonConvert.DeserializeObject<Rootobject>(json);