使用Json.Net解析google map geocode json对对象的响应

问题描述 投票:19回答:6

我有一个满是地址的数据库,我需要得到lat和long,所以我想循环遍历它们并使用Google Geocode来更新我的数据库。我被困在如何解析JSOn结果以获得我需要的东西:

var address = "http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false";
var result = new System.Net.WebClient().DownloadString(address);
GoogleGeoCodeResponse test = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(result);

我以为我可以简单地构建一个快速类并使用JSON.Net来反序列化结果,它有点工作,但我想我在我的类结构上吹它:

public  class GoogleGeoCodeResponse {

    public string status { get; set; }
    public geometry geometry { get; set; }

}

public class geometry {
    public string location_type { get; set; }
    public location location { get; set; }
}

public class location {
    public string lat {get;set;}
    public string lng {get;set;}
}

以下是从Google返回的内容示例:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

我很想简单,我知道,有人吗?

c# json google-maps
6个回答
47
投票

我尝试了这个,做了一个简单的测试,它工作(添加结果和其他):

public class GoogleGeoCodeResponse
{

    public string status { get; set; }
    public results[] results { get; set; }

}

public class results
{
    public string formatted_address { get; set; }
    public geometry geometry { get; set; }
    public string[] types { get; set; }
    public address_component[] address_components { get; set; }
}

public class geometry
{
    public string location_type { get; set; }
    public location location { get; set; }
}

public class location
{
    public string lat { get; set; }
    public string lng { get; set; }
}

public class address_component
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public string[] types { get; set; }
}

20
投票

您可以使用动态对象而不是定义对象。

 public static dynamic GEOCodeAddress(String Address)
    {
        var address = String.Format("http://maps.google.com/maps/api/geocode/json?address={0}&sensor=false", Address.Replace(" ", "+"));
        var result = new System.Net.WebClient().DownloadString(address);
        JavaScriptSerializer jss = new JavaScriptSerializer();
        return jss.Deserialize<dynamic>(result);
    }

4
投票

我做过类似的事情是指Google Geo Kit


3
投票

C#对象代码我添加了一些额外的类,不确定它们是否是API的新手,但我认为这可能对某人有帮助。

public class GoogleGeoCodeResponse
    {
        public results[] results { get; set; }
        public string status { get; set; }

    }

    public class results
    {
        public address_component[] address_components { get; set; }
        public string formatted_address { get; set; }
        public geometry geometry { get; set; }
        public string[] types { get; set; }
    }

    public class address_component
    {
        String long_name { get; set; }
        String short_name { get; set; }
        String types { get; set; }

    }

    public class geometry
    {
        public bounds bounds { get; set; }
        public location location { get; set; }
        public string location_type { get; set; }
        public viewport viewport { get; set; }
    }

    public class location
    {
        public string lat { get; set; }
        public string lng { get; set; }
    }

    public class viewport
    {
        public northeast northeast { get; set; }
        public southwest southwest { get; set; }
    }

    public class bounds
    {
        public northeast northeast { get; set; }
    }

    public class northeast
    {
        public string lat { get; set; }
        public string lng { get; set; }
    }

    public class southwest
    {
        public string lat { get; set; }
        public string lng { get; set; }
    }

2
投票

感谢上面的JEuvin,我能够轻松地从XML切换到Json,并使用一些mod到上面的代码(特别是将lat和lng更改为十进制或双精度),还必须将address_components.types更改为string []以使其更改为为我工作。然后我重构了一下,以便可以互换地从XML或Json反序列化相同的类。

也许这也会帮助别人......

using System;
using System.Xml.Serialization;

[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class GeocodeResponse
{
    public GeocodeResponse()
    {
    }

    [XmlElement("result")]

    public results[] results { get; set; }

    public string status { get; set; }
}

[XmlType(AnonymousType = true)]
public class results
{
    public results()
    {
    }

    [XmlElement("address_component")]

    public address_component[] address_components { get; set; }

    public string formatted_address { get; set; }

    public geometry geometry { get; set; }

    [XmlElement("type")]
    public string[] types { get; set; }

    public string[] postcode_localities { get; set; }

    public bool partial_match { get; set; }

    public string place_id { get; set; }
}

[XmlType(AnonymousType = true)]
public class address_component
{
    public address_component()
    {
    }

    public string long_name { get; set; }

    public string short_name { get; set; }

    [XmlElement("type")]
    public string[] types { get; set; }
}

[XmlType(AnonymousType = true)]
public class geometry
{
    public geometry()
    {
    }

    public bounds bounds { get; set; }

    public location location { get; set; }

    public string location_type { get; set; }

    public viewport viewport { get; set; }
}

[XmlType(AnonymousType = true)]
public class location
{
    public location()
    {
    }

    public double lat { get; set; }

    public double lng { get; set; }
}

[XmlType(AnonymousType = true)]
public class viewport
{
    public viewport()
    {
    }

    public northeast northeast { get; set; }

    public southwest southwest { get; set; }
}

[XmlType(AnonymousType = true)]
public class bounds
{
    public bounds()
    {
    }

    public northeast northeast { get; set; }
}

[XmlType(AnonymousType = true)]
public class northeast
{
    public northeast()
    {
    }

    public double lat { get; set; }

    public double lng { get; set; }
}

[XmlType(AnonymousType = true)]
public class southwest
{
    public southwest()
    {
    }

    public double lat { get; set; }

    public double lng { get; set; }
}

(通过添加postcode_localities和partial_match属性进行编辑)


0
投票

确保该类是Serializable,允许nullables

[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = true)]
public class GeocodeResponse
{
    public GeocodeResponse()
    {
      // can be empty or you can initiate the properties here
    }

    [XmlElement("location ")]
    [Display(Name = "location ")]
    // add json attributes as well
    public location  location { get; set; }

    public string status { get; set; }
  }
© www.soinside.com 2019 - 2024. All rights reserved.