Google 地理编码 Json C# 中的解析问题
Google Geocoding Json Parsing Issue in C#
我的代码工作正常,但我似乎无法到达树的更深部分。我正在尝试拉经度和纬度。下面的代码将 'status' 拉为“OK”没问题(在响应的最后)。'geometry' -> 'location' -> 'lat' 的语法是什么'lng'?
这是我的代码:
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;
这是 API 生成的内容:
{
"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"
}
以下是获得所需内容的步骤:
Post 你的 JSON 在 http://json2csharp.com/。取结果 类 并合并重复项,你会得到:
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; }
}
(您也可以使用 Paste JSON as Classes or https://jsonutils.com/ 来生成您的初始类型定义。)
用 Json.NET 反序列化你的 JSON 像这样:
var root = JsonConvert.DeserializeObject<RootObject>(result);
您的查询返回了多个结果,因此您需要像这样遍历返回的位置:
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.
}
'geometry' -> 'location' -> 'lat' 和 'lng' 的语法是:
JObject data = JObject.Parse(result);
string lat = (string)data["results"][0]["geometry"]["location"]["lat"];
string lng = (string)data["results"][0]["geometry"]["location"]["lng"];
我的代码工作正常,但我似乎无法到达树的更深部分。我正在尝试拉经度和纬度。下面的代码将 'status' 拉为“OK”没问题(在响应的最后)。'geometry' -> 'location' -> 'lat' 的语法是什么'lng'?
这是我的代码:
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;
这是 API 生成的内容:
{
"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"
}
以下是获得所需内容的步骤:
Post 你的 JSON 在 http://json2csharp.com/。取结果 类 并合并重复项,你会得到:
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; } }
(您也可以使用 Paste JSON as Classes or https://jsonutils.com/ 来生成您的初始类型定义。)
用 Json.NET 反序列化你的 JSON 像这样:
var root = JsonConvert.DeserializeObject<RootObject>(result);
您的查询返回了多个结果,因此您需要像这样遍历返回的位置:
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. }
'geometry' -> 'location' -> 'lat' 和 'lng' 的语法是:
JObject data = JObject.Parse(result);
string lat = (string)data["results"][0]["geometry"]["location"]["lat"];
string lng = (string)data["results"][0]["geometry"]["location"]["lng"];