首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Json.Net将谷歌地图地理编码json响应解析为对象

使用Json.Net将谷歌地图地理编码json响应解析为对象
EN

Stack Overflow用户
提问于 2010-06-09 04:41:59
回答 7查看 32.4K关注 0票数 20

我有一个充满了地址的数据库,我需要得到lat和long,所以我想循环通过它们并使用Google地理编码来更新我的数据库。我陷入了如何解析JSOn结果以获得所需内容的困境:

代码语言:javascript
复制
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来反序列化结果,这是一种工作,但我认为我在我的类结构上搞砸了:

代码语言:javascript
复制
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;}
}

以下是get从Google返回的示例:

代码语言:javascript
复制
{
  "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
        }
      }
    }
  } ]
}

我错过了这里的简单我知道这一点,任何人?

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-08-02 23:30:59

我试过了,做了一个简单的测试,它起作用了(添加了结果和其他):

代码语言:javascript
复制
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; }
}
票数 50
EN

Stack Overflow用户

发布于 2011-11-23 08:44:58

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

代码语言:javascript
复制
 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);
    }
票数 20
EN

Stack Overflow用户

发布于 2010-09-23 03:19:09

参考Google Geo Kit,我也做过类似的事情

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3001132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档