问题描述
我有我的code工作正常,但我不能似乎能够得到树的深层部位。我想拉的经度和纬度。在code以下拉'状态'没有问题,因为几何OK(在响应的最后)什么是语法。'' - >'的位置? - >纬度和LNG
下面是我的code:
字符串RawAddress =163 Leektown路,新格雷特纳,NJ 08004;
串地址= RawAddress.Replace(,+);
字符串AddressURL =http://maps.google.com/maps/api/geo$c$c/json?address=+地址;
VAR的结果=新System.Net.WebClient()DownloadString(AddressURL)。
动态数据= JObject.Parse(结果);Lat.Text = data.status;
这是API生成的内容:
{
结果:
{
address_components:[
{
LONG_NAME:山景
SHORT_NAME:山景
类型:本地,政治]
},
{
LONG_NAME:圣克拉拉县
SHORT_NAME:圣克拉拉县
类型:administrative_area_level_2,政治]
},
{
LONG_NAME:加州,
SHORT_NAME:CA,
类型:administrative_area_level_1,政治]
},
{
LONG_NAME:美的,
SHORT_NAME:美国,
类型:国家,政治]
}
]
的formatted_address:山景城,CA,USA
几何体:{
界限:{
东北:{
纬度:37.4508789,
LNG:-122.0446721
},
西南:{
纬度:37.3567599,
LNG:-122.1178619
}
},
位置 : {
纬度:37.3860517,
LNG:-122.0838511
},
LOCATION_TYPE:近似
视口:{
东北:{
纬度:37.4508789,
LNG:-122.0446721
},
西南:{
纬度:37.3567599,
LNG:-122.1178619
}
}
},
partial_match:真实,
类型:本地,政治]
}
]
状态:OK
}
下面是得到你想要的步骤:
-
发表在广告JSON。承担由此产生的类和合并重复,你会得到:
公共类AddressComponent
{
公共字符串LONG_NAME {搞定;组; }
公共字符串SHORT_NAME {搞定;组; }
公开名单<串GT;类型{搞定;组; }
}公共类边界
{
公共场所东北{搞定;组; }
公共场所西南{搞定;组; }
}公共类位置
{
公共双纬度{搞定;组; }
公共双LNG {搞定;组; }
}公共类几何
{
公共边界界限{搞定;组; }
公共场所的位置{搞定;组; }
公共字符串LOCATION_TYPE {搞定;组; }
公共边界视{搞定;组; }
}公共类结果
{
公开名单< AddressComponent> address_components {搞定;组; }
公共字符串的formatted_address {搞定;组; }
公共几何形状{搞定;组; }
公共BOOL partial_match {搞定;组; }
公开名单<串GT;类型{搞定;组; }
}公共类RootObject
{
公开名单<结果>结果{搞定;组; }
公共字符串状态{搞定;组; }
} -
使用像这样反序列化JSON:
VAR根= JsonConvert.DeserializeObject< RootObject>(结果);
-
有多种结果的查询返回的,所以你需要遍历返回的位置,像这样:
的foreach(在root.results VAR singleResult)
{
VAR位置= singleResult.geometry.location;
VAR纬度= location.lat;
VAR经度= location.lng;
//做你想做他们。
}
I have my code working fine, but I can't seem to be able to get to the deeper parts of the tree. I'm trying to pull the longitude and the latitude. The code below pulls 'status' no problem as 'OK" (at the very end of the response). What is the syntax for 'geometry' -> 'location' -> 'lat' and 'lng'?
Here is my code:
string RawAddress = "163 Leektown Road, New Gretna, NJ 08004";
string Address = RawAddress.Replace(" ", "+");
string AddressURL = "http://maps.google.com/maps/api/geocode/json?address=" + Address;
var result = new System.Net.WebClient().DownloadString(AddressURL);
dynamic data = JObject.Parse(result);
Lat.Text = data.status;
This is what the API generates:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Mountain View, CA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.4508789,
"lng" : -122.0446721
},
"southwest" : {
"lat" : 37.3567599,
"lng" : -122.1178619
}
},
"location" : {
"lat" : 37.3860517,
"lng" : -122.0838511
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 37.4508789,
"lng" : -122.0446721
},
"southwest" : {
"lat" : 37.3567599,
"lng" : -122.1178619
}
}
},
"partial_match" : true,
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
Here are the steps to get what you want:
Post your JSON in http://json2csharp.com/. Take the resulting classes and merge duplicates, and you get:
public class AddressComponent { public string long_name { get; set; } public string short_name { get; set; } public List<string> types { get; set; } } public class Bounds { public Location northeast { get; set; } public Location southwest { get; set; } } public class Location { public double lat { get; set; } public double lng { get; set; } } public class Geometry { public Bounds bounds { get; set; } public Location location { get; set; } public string location_type { get; set; } public Bounds viewport { get; set; } } public class Result { public List<AddressComponent> address_components { get; set; } public string formatted_address { get; set; } public Geometry geometry { get; set; } public bool partial_match { get; set; } public List<string> types { get; set; } } public class RootObject { public List<Result> results { get; set; } public string status { get; set; } }
Deserialize your JSON with Json.NET like so:
var root = JsonConvert.DeserializeObject<RootObject>(result);
There are multiple results returned for your query, so you need to loop through the locations returned like so:
foreach (var singleResult in root.results) { var location = singleResult.geometry.location; var latitude = location.lat; var longitude = location.lng; // Do whatever you want with them. }
这篇关于谷歌地理编码JSON解析问题在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!