Sle*_*lee 20 c# json google-maps
我有一个满是地址的数据库,我需要得到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);
Run Code Online (Sandbox Code Playgroud)
我以为我可以简单地构建一个快速类并使用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;}
}
Run Code Online (Sandbox Code Playgroud)
以下是从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
}
}
}
} ]
}
Run Code Online (Sandbox Code Playgroud)
我很想简单,我知道,有人吗?
Ant*_*nio 48
我尝试了这个,做了一个简单的测试,它工作(添加结果和其他):
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; }
}
Run Code Online (Sandbox Code Playgroud)
小智 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30174 次 |
| 最近记录: |