我正在尝试反序列化谷歌地图地理代码信息。我很接近了,但我还缺了点什么。
以下是JSON输出
{
"results" : [
{
"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" : "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" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
这是我的类:
Public Class AddressComponent
Public Property LongName As String
Public Property ShortName As String
Public Property Types As String()
End Class
Public Class Location
Public Property Lat As Double
Public Property Lng As Double
End Class
Public Class Northeast
Public Property Lat As Double
Public Property Lng As Double
End Class
Public Class Southwest
Public Property Lat As Double
Public Property Lng As Double
End Class
Public Class Viewport
Public Property Northeast As Northeast
Public Property Southwest As Southwest
End Class
Public Class Geometry
Public Property Location As Location
Public Property LocationType As String
Public Property Viewport As Viewport
End Class
Public Class Result
Public Property AddressComponents As AddressComponent()
Public Property FormattedAddress As String
Public Property Geometry As Geometry
Public Property PlaceId As String
Public Property Types As String()
End Class
Public Class GeocodeClass
Public Property Results As Result()
Public Property Status As String
End Class
这是我的代码,我一直在摆弄
Try
Dim url As String = "https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=KEY_OMITTED"
Dim wc As New WebClient()
Dim json = DirectCast(JsonConvert.DeserializeObject(wc.DownloadString(url)), JObject)
Dim formattedaddress = json("results").SelectMany(Function(x) x("address_components")).FirstOrDefault(Function(t) t("types").First().ToString() = "country")
'Dim country = json("results").SelectMany(Function(x) x("address_components")).FirstOrDefault(Function(t) t("types").First().ToString() = "country")
Dim addressinfo As GeocodeClass = JsonConvert.DeserializeObject(Of GeocodeClass)(json)
TextBox2.Text = formattedaddress.ToString
Catch ex As Exception
End Try
End Sub
我能够从"country"字符串中获得输出。我的目标是发出请求并将数据插入到数据库中。我很接近了,但我还是很迷茫。有人能给我指个方向吗?JSON是一个新概念,c#中有很多例子,不幸的是,vb.net对我来说更熟悉。
您几乎拥有将其反序列化为对象的所有内容,但是您不需要对字符串进行任何预处理。在一个机器人为你创建的课堂上,你还"修理"了太多东西。
如果你看一下json,它清楚地显示了一个名为address_components
的属性-当你将其更改为AddressComponents
时,这些部分将返回为Nothing/null,因为属性似乎缺失了(它们是!)。从long_name
到LongName
也是一样。
可以更改类名,但不能更改属性。为此,使用JsonProperty
属性:
<JsonProperty("lat")>
Public Property Latitude As Single
这基本上创建了一个别名,这样反序列化器可以使用"lat"
,而你可以自由地使用Latitude
。
此外,当涉及到为您创建类(甚至VS)时,所有的机器人都有点低效。您可能已经注意到Location
、Northeast
和Southwest
是相同的。这意味着您可以对它们使用相同的类。修改后的类注意,我保留了一些属性名称,如"results":
Public Class GeocodeClass
Public Property results As Result()
Public Property status As String
End Class
Public Class Result
<JsonProperty("address_components")>
Public Property AddressComponents As AddressComponents()
<JsonProperty("formatted_address")>
Public Property FormattedAddress As String
<JsonProperty("Geometry")>
Public Property geometry As Geometry
Public Property place_id As String
Public Property types As String()
End Class
Public Class Geometry
Public Property location As Location
Public Property location_type As String
Public Property viewport As Viewport
End Class
' class for Location, ViewPort.Northeast, ViewPort.southwest
Public Class Location
<JsonProperty("lat")>
Public Property Lat As Single
<JsonProperty("lng")>
Public Property Lng As Single
End Class
Public Class Viewport
<JsonProperty("northeast")>
Public Property Northeast As Location
<JsonProperty("southwest")>
Public Property Southwest As Location
End Class
Public Class AddressComponents
<JsonProperty("long_name")>
Public Property LongName As String
<JsonProperty("short_name")>
Public Property ShortName As String
Public Property types As String()
End Class
然后反序列化:
Dim jstr = ....the json from whereever
Dim geoInfo = JsonConvert.DeserializeObject(Of GeocodeClass)(jstr)
' == "California"
Console.WriteLine(geoInfo.results(0).AddressComponents(4).LongName)
最外层的类没有太多的信息,它创建了一个图层来导航。消除它的一种方法:
Dim geoAddress() As Result ' a bad Type name
Dim jobj = JObject.Parse(jstr)
If jobj("status").ToString = "OK" Then
geoAddress = JsonConvert.DeserializeObject(Of Result())(jobj("results").ToString())
End If
' also prints "California"
Console.WriteLine(geoAddress(0).AddressComponents(4).LongName)
如果状态(在本例中不是" status ")是OK的,那么它只是将结果(不是" results ")部分反序列化到一个数组中,这样您就不再需要用geoInfo.
来开头了。使用第二个,您不再需要GeocodeClass