反序列化Google Maps Geocode JSON信息 [英] Deserialize Google maps Geocode JSON information
问题描述
我正在尝试反序列化Google地图地理编码信息.我很近,但是我想念一些东西.
I am trying to deserialize google map geocode information. I am close but I am missing something.
这是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
这是我一直在搞乱的代码
And here's my code I am have been messing with
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
我能够从国家"字符串中获得输出.我的目标是提出请求并将数据插入数据库中.我很近但是我仍然很迷路.有人可以向我轻推吗? JSON是一个新概念,C#中有许多示例,但不幸的是vb.net对我来说更熟悉.
I am able to get an output from the "country" string. My goal is to make a request and insert the data in to a database. I am close but I am still pretty lost. Can someone nudge me in a direction? JSON is a new concept and so many examples are in C# and unfortunately vb.net is more familiar to me.
推荐答案
您几乎可以使用 将其反序列化为对象,但是您无需执行任何操作 >对该字符串进行预处理.您还会在为您创建的机器人之一的类中修复"太多的东西.
You almost have everything in place to Deserialize it to an object, but you dont need to do any of that pre-processing to the string. You also "fixed up" too many things in the classes one of the robots created for you.
如果您查看json,它将清楚地显示一个名为address_components
的属性-当您将其更改为AddressComponents
时,这些部分将以Nothing/null的形式返回,因为该属性似乎丢失了(它们是!). long_name
至LongName
也是如此.
If you look at the json, it clearly shows a property named address_components
- when you changed that to AddressComponents
those parts will come back as Nothing/null because the properties appear to be missing (they are!). The same applies to long_name
to LongName
.
可以更改类名,但不能更改属性.为此,请使用JsonProperty
属性:
Its OK to change class names, but not properties. For that, use the JsonProperty
attribute:
<JsonProperty("lat")>
Public Property Latitude As Single
这基本上会创建一个别名,以便反序列化程序可以使用"lat"
,而让您可以自由使用Latitude
.
This basically creates an alias so the deserializer can use "lat"
and leaves you free to use Latitude
.
此外,在为您创建类(甚至是VS)时,所有机器人的效率都有些低下.您可能已经注意到Location
,Northeast
和Southwest
是相同的.这意味着您可以对所有它们使用相同的类.修订后的类请注意,我只留下了一些属性名称,例如结果":
Also, all the robots are a bit inefficient when it comes to creating the classes for you (even VS). You might have noticed that Location
, Northeast
and Southwest
are identical. This means you can use the same class for all of them. Revised classes note that I left some of the property names alone like "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)
该最外层的类中没有太多信息,因此它又创建了一层导航.摆脱它的一种方法:
That outermost class does not have much info in it, and it creates one more layer to navigate. One way to get rid of it:
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
If the status (not "Status" in this case) is OK, then it just deserializes the results (not "Results") portion into an array so you no longer have to preface everything with geoInfo.
Using the second, you no longer need the GeocodeClass
这篇关于反序列化Google Maps Geocode JSON信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!