2015-04-04 92 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