使用Json.Net解析Google Maps Geocode API [英] Parse Google Maps Geocode API Using Json.Net
问题描述
我认为我可以简单地构建一个快速类并使用JSON.Net来反序列化结果,而且它可以工作,但我认为我正在将它应用到我的类结构上:
public class Location
{
public string lat {get;组; }
public string lng {get;组; }
}
public class Geometry
{
public Location location {get;组; }
}
public class Json
{
public Results results {get;组; }
}
public class Results
{
public Json json {get;组; }
public几何几何{get;组; }
}
public class Query
{
public Results results {get;组; }
}
public class RootObject
{
public Query query {get;组; }
}
以下是Google返回的样例:
{
results:[
{
address_components:[
{
long_name:1600,
short_name:1600,
types:[street_number]
},
{
long_name:Amphitheatre Parkway,
short_name:Amphitheatre Pkwy,
types:[route]
},
{
long_name:Mountain View,
short_name:Mountain View,
types:[locality,political]
},
{
long_name:Santa Clara,
short_name:Santa Clara,
types:[administrative_area_level_2,political]
},
{
long_name:California,
short_name:CA,
types:[administrative_area_level_1,political]
},
long_name:美国,
short_name:美国,
types:[country,political]
} ,
{
long_name:94043,
short_name:94043,
types:[postal_code]
}
$ b格式化地址:1600 Amphitheatre Parkway,Mountain View,CA 94043,USA,
geometry:{
location:{
lat :37.42244920,
lng:-122.08506440
},
location_type:ROOFTOP,
viewport:{
northeast:{
lat:37.42379818029150,
lng:-122.0837154197085
},
southwest:{
lat:37.42110021970850,
lng:-122.0864133802915
}
}
},
types:[street_address]
}
],
status:OK
}
我很想念这里,我知道它,任何人?
请尝试使用 json2csharp.com 来生成您的课程。使用这个工具,类结构如下所示:
public class AddressComponent
{
public string long_name {get;组; }
public string short_name {get;组; }
public List< string> types {get;组; }
}
公共类位置
{
public double lat {get;组; }
public double lng {get;组; }
}
公共类东北部
{
公共双lat {get;组; }
public double lng {get;组; }
}
公共类西南
{
public double lat {get;组; }
public double lng {get;组; }
}
public class Viewport
{
public northeast northeast {get;组; }
public西南西南{get;组; }
}
public class Geometry
{
public Location location {get;组; }
public string location_type {get;组; }
public Viewport viewport {get;组; }
}
public class Result
{
public List< AddressComponent> address_components {get;组; }
公共字符串formatted_address {get;组; }
public几何几何{get;组; }
public List< string> types {get;组; }
}
public class RootObject
{
public List< Result>结果{get;组; }
public string status {get;组; }
}
请注意,您可以稍微模仿一下:生成的 Northeast
和 Southwest
类与位置
相同,因此您可以将它们替换为位置
它们在 Viewport
类中使用。
I thought I could simply build a quick class and use JSON.Net to deserialize the result, and it is kind of working but I think I am blowing it on my class structure:
public class Location
{
public string lat { get; set; }
public string lng { get; set; }
}
public class Geometry
{
public Location location { get; set; }
}
public class Json
{
public Results results { get; set; }
}
public class Results
{
public Json json { get; set; }
public Geometry geometry { get; set; }
}
public class Query
{
public Results results { get; set; }
}
public class RootObject
{
public Query query { get; set; }
}
Here is a sample of what get's returned from Google:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara",
"short_name" : "Santa Clara",
"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" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.42244920,
"lng" : -122.08506440
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.42379818029150,
"lng" : -122.0837154197085
},
"southwest" : {
"lat" : 37.42110021970850,
"lng" : -122.0864133802915
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
I am missing simple here I know it, anyone?
Try using json2csharp.com to generate your classes. Using this tool, the class structure comes out like this:
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Geometry
{
public Location location { get; set; }
public string location_type { get; set; }
public Viewport 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 List<string> types { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
public string status { get; set; }
}
Note that you can simiplify this a little: the generated Northeast
and Southwest
classes are identical to Location
, so you can replace them with Location
where they are used inside the Viewport
class.
这篇关于使用Json.Net解析Google Maps Geocode API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!