JavaScriptSerializer 无法解析数据类型
我有一些如下所示的 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)
I have some JSON data that looks like this:
{
"data":
[{
"name":"John Smith",
"id":"12345"
}]
}
I have a pair of serializeable classes like so:
<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
When I try to deserialize as an object:
Dim serializer As New JavaScriptSerializer()
Dim userResult As Object = serializer.DeserializeObject(json)
I get one root object with key "data", and value another object with 2 children, with keys "name" and "id", and the appropriate values one might expect. But when I try to cast that object to my UserData
type, it returns Nothing
. I had this code working at some point, but now that I am returning to it and attempting to use it again, it seems some code rot has set in and it has stopped working.
Here is how I am attempting to get the deserialized data as a UserData
object:
Dim userResult As UserData = TryCast(serializer.DeserializeObject(json), UserData)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过更改
为“
不完全确定这里的功能差异是什么”来解决此问题,但这得到了我想要的结果。
I was able to fix this issue by changing
to
Not exactly sure what the functional difference here is, but that got me the result i wanted.