反序列化JSON vb.net响应google maps api

反序列化JSON vb.net响应google maps api,vb.net,Vb.net,这里需要帮助。 我不会在vb.net中反序列化这个json。 我需要lat值:-21.4105261和lng值:-42.1956855 { "results" : [ { "address_components" : [ { "long_name" : "28460-000", "short_name" : "28460-000", "types"

这里需要帮助。 我不会在vb.net中反序列化这个json。 我需要lat值:-21.4105261和lng值:-42.1956855

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "28460-000",
               "short_name" : "28460-000",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Rua Francisco Cardoso, 25 - Morro do Demétrio, Miracema - RJ, 28460-000, Brazil",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                 "lat" : -21.4105261,
                  "lng" : -42.1956855
               },
               "southwest" : {
                  "lat" : -21.4105429,
                  "lng" : -42.1956892
               }
            },
            "location" : {
               "lat" : -21.4105429,
               "lng" : -42.1956892
            },
            "location_type" : "RANGE_INTERPOLATED",
            "viewport" : {
               "northeast" : {
                  "lat" : -21.4091855197085,
                  "lng" : -42.1943383697085
               },
               "southwest" : {
                  "lat" : -21.4118834802915,
                  "lng" : -42.1970363302915
               }
            }
         },
         "place_id" : "ElBSdWEgRnJhbmNpc2NvIENhcmRvc28sIDI1IC0gTW9ycm8gZG8gRGVtw6l0cmlvLCBNaXJhY2VtYSAtIFJKLCAyODQ2MC0wMDAsIEJyYXNpbA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}
有人能帮忙吗? 这是我第一次使用Json,所以我对它没有经验。
Ps:我已经安装了json.net库。

首先,您需要创建一个自定义类来表示要反序列化的对象。。然后你会做一些像


Dim jss=新的JavaScriptSerializer
Dim resp As List(属于cls_custom_类)=jss.Deserialize(属于cls_custom_类的列表))(json)


如果您正确构建了类,反序列化函数应该能够将JSON映射到您的自定义对象,然后您就可以完全访问所需的任何字段/值。

您可以使用JSON.net并反序列化JSON。 1/创建模型

Public Class GooglCitiesResult
    Public Property city As String
    Public Property country As String
    Public Property country_code As String
End Class
Public Class GoogleGeoCodeResponse
    Public Property status As String
    Public Property results As results()
End Class
Public Class results
    Public Property formatted_address As String
    Public Property geometry As geometry
    Public Property types As String()
    Public Property address_components As address_component()
End Class
Public Class geometry
    Public Property location_type As String
    Public Property location As location
End Class

Public Class location
    Public Property lat As String
    Public Property lng As String
End Class

Public Class address_component
    Public Property long_name As String
    Public Property short_name As String
    Public Property types As String()
End Class
2/在控制器中调用它

GoogleUrl = "http://maps.google.com/maps/api/geocode/json?address=Paris,France&sensor=false&language=en"
Dim client As WebClient = New WebClient()
client.Encoding = System.Text.Encoding.UTF8
Dim result = Await client.DownloadStringTaskAsync(GoogleUrl)
Dim jsonGoogle As GoogleGeoCodeResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(Of GoogleGeoCodeResponse)(result)

Dim GoogleLatitude As String = jsonGoogle.results(0).geometry.location.lat
Dim GoogleLongitude As String = jsonGoogle.results(0).geometry.location.lng

欢迎来到堆栈溢出!请花点时间仔细阅读以下内容: