Parse google maps geocode json ответ на объект с использованием Json.Net
У меня есть БД, полная адресов, которые мне нужны, чтобы получить лат и долго, поэтому я хочу пропустить их и использовать 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
}
}
}
} ]
}
Мне просто не хватает здесь, я знаю это, кто-нибудь?
Ответы
Ответ 1
Я попробовал это, сделал простой тест, и он сработал (добавил результаты и другие):
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; }
}
Ответ 2
Вы можете использовать динамический объект, а не определять объект.
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);
}
Ответ 3
Я сделал нечто похожее
обратитесь к Google Geo Kit
Ответ 4
Код объекта С#
Я добавил несколько дополнительных классов, не уверен, что они новичок в API, но я подумал, что это может быть полезно кому-то.
public class GoogleGeoCodeResponse
{
public results[] results { get; set; }
public string status { get; set; }
}
public class results
{
public address_component[] address_components { get; set; }
public string formatted_address { get; set; }
public geometry geometry { get; set; }
public string[] types { get; set; }
}
public class address_component
{
String long_name { get; set; }
String short_name { get; set; }
String types { get; set; }
}
public class geometry
{
public bounds bounds { get; set; }
public location location { get; set; }
public string location_type { get; set; }
public viewport viewport { get; set; }
}
public class location
{
public string lat { get; set; }
public string lng { get; set; }
}
public class viewport
{
public northeast northeast { get; set; }
public southwest southwest { get; set; }
}
public class bounds
{
public northeast northeast { get; set; }
}
public class northeast
{
public string lat { get; set; }
public string lng { get; set; }
}
public class southwest
{
public string lat { get; set; }
public string lng { get; set; }
}
Ответ 5
Благодаря JEuvin выше, я смог легко переключиться с XML на Json с несколькими модами на указанный выше код (в частности, изменить lat и lng на десятичные или двойные), а также пришлось изменить address_components.types на строку [] на заставить его работать для меня. Затем я немного реорганизовал так, что одни и те же классы можно десериализовать из XML или Json взаимозаменяемо.
Может быть, это тоже поможет кому-то...
using System;
using System.Xml.Serialization;
[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class GeocodeResponse
{
public GeocodeResponse()
{
}
[XmlElement("result")]
public results[] results { get; set; }
public string status { get; set; }
}
[XmlType(AnonymousType = true)]
public class results
{
public results()
{
}
[XmlElement("address_component")]
public address_component[] address_components { get; set; }
public string formatted_address { get; set; }
public geometry geometry { get; set; }
[XmlElement("type")]
public string[] types { get; set; }
public string[] postcode_localities { get; set; }
public bool partial_match { get; set; }
public string place_id { get; set; }
}
[XmlType(AnonymousType = true)]
public class address_component
{
public address_component()
{
}
public string long_name { get; set; }
public string short_name { get; set; }
[XmlElement("type")]
public string[] types { get; set; }
}
[XmlType(AnonymousType = true)]
public class geometry
{
public geometry()
{
}
public bounds bounds { get; set; }
public location location { get; set; }
public string location_type { get; set; }
public viewport viewport { get; set; }
}
[XmlType(AnonymousType = true)]
public class location
{
public location()
{
}
public double lat { get; set; }
public double lng { get; set; }
}
[XmlType(AnonymousType = true)]
public class viewport
{
public viewport()
{
}
public northeast northeast { get; set; }
public southwest southwest { get; set; }
}
[XmlType(AnonymousType = true)]
public class bounds
{
public bounds()
{
}
public northeast northeast { get; set; }
}
[XmlType(AnonymousType = true)]
public class northeast
{
public northeast()
{
}
public double lat { get; set; }
public double lng { get; set; }
}
[XmlType(AnonymousType = true)]
public class southwest
{
public southwest()
{
}
public double lat { get; set; }
public double lng { get; set; }
}
(редактируется с добавлением свойств postcode_localities и partial_match)
Ответ 6
Убедитесь, что класс Serializable, разрешите nullables
[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = true)]
public class GeocodeResponse
{
public GeocodeResponse()
{
// can be empty or you can initiate the properties here
}
[XmlElement("location ")]
[Display(Name = "location ")]
// add json attributes as well
public location location { get; set; }
public string status { get; set; }
}