我有一些如下所示的 JSON 数据:
{
"data":
[{
"name":"John Smith",
"id":"12345"
}]
}
我有一对可序列化的类,如下所示:
<Serializable()> _
Public Class User
Private _name As String
Private _id As String
Public Property name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property id() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property
End Class
<Serializable()>
Public Class UserData
Private _data As List(Of User)
Public Property data() As List(Of User)
Get
Return (_data)
End Get
Set(ByVal value As List(Of User))
_data = value
End Set
End Property
End Class
当我尝试反序列化为对象时:
Dim serializer As New JavaScriptSerializer()
Dim userResult As Object = serializer.DeserializeObject(json)
我得到一个带有键“data”的根对象,并为另一个带有 2 个子对象的对象赋值,键为“name”和“id”,以及人们可能期望的适当值。但是当我尝试将该对象转换为我的UserData
类型时,它会返回Nothing
. 我让这段代码在某个时候工作,但现在我回到它并尝试再次使用它,似乎一些代码腐烂已经进入并且它已经停止工作。
这是我尝试将反序列化数据作为UserData
对象获取的方式:
Dim userResult As UserData = TryCast(serializer.DeserializeObject(json), UserData)