我正在尝试反序列化某些路线API返回的json,类似于Google Maps API。我的JSON如下(我使用的是VB.NET 2008):
jsontext = { “版本”:0.3, “状态”:0, “route_summary”: { “total_distance”:300, “TOTAL_TIME”:14, “start_point”: “43”, “end_point”: “42” }, “route_geometry”:[[51.025421,18.647631],[51.026131,18.6471],[51.027802,18.645639]] “route_instructions”:[[“西北43号”,88,0,4,“88米”,“西北”,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。它充满了“没有”。其他属性,如“status”或“version”都可以正确填充。
任何想法,如何为上面的JSON定义类?
你能分享一些用于在VB.NET中反序列化JSON的工作代码吗?
谢谢,
答案 0 :(得分:2)
下面是一个示例Converter类,它将获取传入的JSON流并转换为您指定的对象。我应该注意下面的代码是.Net 4.0。 4中的JSON序列化器更容易使用。如果你不能使用4,请告诉我,我会看看我是否可以挖掘3.5版本。基本上,您需要创建一个类结构来将JSON映射到Class。我为你创建了Route和RouteSummary类。我把route_geometry和route_instructions留作了对象。您应该为每个创建类定义,但这应该可以帮助您入门。
Imports System.IO
Imports System.Runtime.Serialization.Json
Imports System.Runtime.Serialization
<DataContract(Namespace:="")> _
Public Class Route
<DataMember(Name:="version")>
Public Property version As Double
<DataMember(Name:="status")>
Public Property status As Double
<DataMember(Name:="route_summary")>
Public Property route_summary As route_summary
<DataMember(Name:="route_geometry")>
Public Property route_geometry As Object()
<DataMember(Name:="route_instructions")>
Public Property route_instructions() As Object
End Class
<DataContract(Name:="route_summary", Namespace:="")> _
Public Class route_summary
<DataMember(Name:="total_distance")>
Public Property total_distance As Double
<DataMember(Name:="total_time")>
Public Property total_time As Double
<DataMember(Name:="start_point")>
Public Property start_point As Double
<DataMember(Name:="end_point")>
Public Property end_point As Double
End Class
Public Class Converter(Of t)
Public Function ReturnJSON(ByRef sreader As StreamReader) As t
If GetType(t).Equals(GetType(String)) Then
Dim result As Object = sreader.ReadToEnd.Replace("""", "")
Return result
Else
Dim ds As New DataContractJsonSerializer(GetType(t))
Dim result As t = DirectCast(ds.ReadObject(sreader.BaseStream), t)
ds = Nothing
Return result
End If
End Function
End Class
Sub Main()
Dim json As String = "{""version"":0.3, ""status"":0, ""route_summary"": { ""total_distance"":300, ""total_time"":14, ""start_point"":""43"", ""end_point"":""42"" }, ""route_geometry"":[[51.025421,18.647631],[51.026131,18.6471],[51.027802,18.645639]], ""route_instructions"": [[""Head northwest on 43"",88,0,4,""88 m"",""NW"",334.8],[""Continue on 42"",212,1,10,""0.2 km"",""NW"",331.1,""C"",356.3]]}"
Dim encoding As New System.Text.UTF8Encoding
Dim bytes() As Byte = encoding.GetBytes(json)
Using os As New MemoryStream
os.Write(bytes, 0, bytes.Length)
os.Position = 0
Using reader As New StreamReader(os)
Dim converter As New Converter(Of Route)
Dim output As Route
output = converter.ReturnJSON(reader)
'output contains data
End Using
End Using
End Sub
有关如何读取JSON数据的详细说明,请参阅此页面。 http://www.json.org/