如何解析谷歌地图API与JSON的响应?

时间:2012-12-04 13:03:11

标签: 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/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答案解析为Distance类:

public struct Distance
{
   // Here I want to parse the distance and duration
}

以下是我收到的json响应示例: http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC&destinations=San+Francisco&mode=bicycling&language=fr-FR&sensor=false

如何将距离和持续时间解析为距离类?
这是我第一次使用Json,所以我没有经验。

Ps:我安装了json.net库。

4 个答案:

答案 0 :(得分:5)

Web Essentials可以选择“将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);

答案 1 :(得分:2)

Visual Studio 2015更新

作为Wouter de Kort的helpful answer的补充,这是Visual Studio 2015 +的更新。

Visual Studio 2015+具有Wouter友好描述的方便的“将JSON粘贴为类”。

为GoogleMaps API JSON反序列化生成所需的类您自己

  1. 在Web浏览器或REST客户端中,导航到填充了一些测试参数的GoogleMaps API URL。例如:https://maps.googleapis.com/maps/api/distancematrix/json?origins=30.3449153,-81.860543&destinations=33.7676932,-84.4906437&language=en-US&units=imperial&key=<API_KEY>
  2. 将生成的原始JSON响应从浏览器/客户端复制(Ctrl+C)到剪贴板。
  3. 在Visual Studio 2015+中,打开要在其中创建类的.cs文件。
  4. 将光标放在您希望生成类的.cs文件中。
  5. 从Visual Studio的菜单中选择编辑&gt;选择性粘贴&gt;将JSON粘贴为类
  6. 结果类的示例

    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; }
    }
    

    从那时起,您可以按照Wouter的其他建议进行操作。

    Rootobject result = JsonConvert.DeserializeObject<Rootobject>(json);
    

答案 2 :(得分:0)

现在已经晚了但对某人有用。在我们有JSON字符串并希望有正确的类结构来正确反序列化的情况下,您可以使用在线工具http://json2csharp.com/

只需粘贴您的JSON字符串,然后点击生成按钮即可。它将为您提供所需的类结构。就这么简单。当您想要避免安装外部工具时,此方法很有用。

答案 3 :(得分:-1)

“距离”类型需要正确建模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);

应该没问题