我有一些看起来像这样的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)
答案 0 :(得分:0)
我可以通过更改
来解决此问题Dim userResult As UserData = TryCast(serializer.DeserializeObject(json), UserData)
到
Dim userResult As UserData = serializer.Deserialize(Of UserData)(json)
不完全确定这里的功能差异是什么,但这让我得到了我想要的结果。