2015-04-04 62 views
-1

需要一些帮助在这里。 我没有反序列化vb.net中的这个json。 我需要值lat:-21.4105261和lng:-42.1956855。反序列化JSON vb.net响应谷歌地图API

{ 
    "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库。

+0

欢迎堆栈溢出!请花一点时间仔细阅读*:[问] – Plutonix 2015-04-04 20:19:10

回答

0

首先,你需要创建一个自定义的类来表示你正在寻找反序列化对象..然后你会做这样的事情......

Dim jss = New JavaScriptSerializer Dim resp As List(Of cls_custom_class) = jss.Deserialize(Of List(Of cls_custom_class))(json)

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

0

您可以使用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