c# - 使用 Json.Net 解析谷歌地图地理编码 json 对对象的响应

标签 c# json google-maps

我有一个数据库,里面装满了我需要获取经纬度的地址,所以我想遍历它们并使用 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
        }
      }
    }
  } ]
}

我在这里缺少简单的我知道,有人吗?

最佳答案

我试过这个,做了一个简单的测试并且成功了(添加了结果和其他):

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

关于c# - 使用 Json.Net 解析谷歌地图地理编码 json 对对象的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3001132/

相关文章:

c# - 正则表达式用于清除分隔符之间文本中的数据

c# - NuGet添加引用错误---无法安装包

php - 如何使用 PHP 对 JSON 数组进行排序

json - 解码对象,其中一个对象是JSON字符串

javascript - 为什么我的有效 kml 在加载到谷歌地图时显示为 "INVALID_DOCUMENT"

c# - 将文件复制到另一个文件夹结构

c# - 无法理解 C# 中的用户控件

javascript - 如何使用 jQuery 在 <a> 中插入 <image>?

mysql - 用户更改位置后,使用 Intel XDK、Google Map API 和 Cordova 将当前位置保存到数据库

javascript - 如何在另一个选项卡中使用 JavaScript 中的坐标打开 Google map ?