我研究过这样的其他问题,我没有做正确的事情。 我的vb.net类是错了或什么。 如果我的类只尝试从候选人或什么代表什么呢? 我需要反序列化JSON此帮助:
{
"spatialReference" : {
"wkid" : 2286
},
"candidates" : [
{
"address" : "100 MAIN ST",
"location" : {
"x" : 1144782.9490543604,
"y" : 81361.525678694248
},
"score" : 100,
"attributes" : {
}
},
{
"address" : "100 E MAIN ST",
"location" : {
"x" : 1120908.3257195801,
"y" : 169917.71846333146
},
"score" : 77,
"attributes" : {
}
}
]
}
我使用下面的代码进行反序列化:
Public Shared Function Deserialise(Of T)(ByVal json As String) As T
Dim obj As T = Activator.CreateInstance(Of T)()
Using ms As MemoryStream = New MemoryStream(Encoding.Unicode.GetBytes(json))
Dim serializer As DataContractJsonSerializer = New DataContractJsonSerializer(obj.GetType())
obj = serializer.ReadObject(ms)
Return obj
End Using
End Function
而我的vb.net类如下所示:
<DataContract()> _
Public Class SearchResults
Private mCandidates() As candidate
<DataContract()> _
Public Class SpatialReference
Private mwkId As String
<DataMember()> _
Public Property wkid() As String
Get
Return mwkId
End Get
Set(ByVal value As String)
mwkId = value
End Set
End Property
End Class
<DataMember()> _
Public Property Candidates() As candidate()
Get
Return mCandidates
End Get
Set(ByVal value As candidate())
mCandidates = value
End Set
End Property
End Class
<DataContract()> _
Public Class candidate
Private mAddress As String
Private mLocation As Location
Private mScore As String
Private mAttr As String
<DataMember()> _
Public Property address() As String
Get
Return mAddress
End Get
Set(ByVal value As String)
mAddress = value
End Set
End Property
<DataMember()> _
Public Property location() As Location
Get
Return mLocation
End Get
Set(ByVal value As Location)
mLocation = value
End Set
End Property
<DataMember()> _
Public Property score() As String
Get
Return mScore
End Get
Set(ByVal value As String)
mScore = value
End Set
End Property
<DataMember()> _
Public Property attributes() As String
Get
Return mAttr
End Get
Set(ByVal value As String)
mAttr = value
End Set
End Property
End Class