我正在尝试反序列化某些类似于 Google Maps API 的方向 API 返回的 json。我的 JSON 如下(我使用的是 VB.NET 2008):
jsontext = {“版本”:0.3,“状态”:0,“路线摘要”:{“总距离”:300,“总时间”:14,“起点”:“43”,“终点”:“42”},“ route_geometry":[[51.025421,18.647631],[51.026131,18.6471],[51.027802,18.645639]], "route_instructions": [["向西北行驶 43",88,0,4,"88 m","NW" ,334.8],["继续 42",212,1,10,"0.2 公里","NW",331.1,"C",356.3]] }
到目前为止,我想出了以下代码:
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim lstTextAreas As Output_CloudMade() = js.Deserialize(Of Output_CloudMade())(jsontext)
我不知道如何定义复杂的类,即Output_CloudMade。
我正在尝试类似的东西:
Public Class RouteSummary
Private mTotalDist As Long
Private mTotalTime As Long
Private mStartPoint As String
Private mEndPoint As String
Public Property TotalDist() As Long
Get
Return mTotalDist
End Get
Set(ByVal value As Long)
mTotalDist = value
End Set
End Property
Public Property TotalTime() As Long
Get
Return mTotalTime
End Get
Set(ByVal value As Long)
mTotalTime = value
End Set
End Property
Public Property StartPoint() As String
Get
Return mStartPoint
End Get
Set(ByVal value As String)
mStartPoint = value
End Set
End Property
Public Property EndPoint() As String
Get
Return mEndPoint
End Get
Set(ByVal value As String)
mEndPoint = value
End Set
End Property
End Class
Public Class Output_CloudMade
Private mVersion As Double
Private mStatus As Long
Private mRSummary As RouteSummary
'Private mRGeometry As RouteGeometry
'Private mRInstructions As RouteInstructions
Public Property Version() As Double
Get
Return mVersion
End Get
Set(ByVal value As Double)
mVersion = value
End Set
End Property
Public Property Status() As Long
Get
Return mStatus
End Get
Set(ByVal value As Long)
mStatus = value
End Set
End Property
Public Property Summary() As RouteSummary
Get
Return mRSummary
End Get
Set(ByVal value As RouteSummary)
mRSummary = value
End Set
End Property
'Public Property Geometry() As String
' Get
' End Get
' Set(ByVal value As String)
' End Set
'End Property
'Public Property Instructions() As String
' Get
' End Get
' Set(ByVal value As String)
' End Set
'End Property
End Class
但它不起作用。问题在于复杂的属性,例如 route_summary。它充满了“无”。其他属性,如“状态”或“版本”已正确填写。
任何想法,如何为上述 JSON 定义类?
你能分享一些在 VB.NET 中反序列化 JSON 的工作代码吗?
谢谢,