JavaScriptSerializer 无法解析数据类型

发布于 2024-12-05 07:29:29 字数 1460 浏览 1 评论 0原文

我有一些如下所示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2024-12-12 07:29:29

我能够通过更改

Dim userResult As UserData = TryCast(serializer.DeserializeObject(json), UserData)

为“

Dim userResult As UserData = serializer.Deserialize(Of UserData)(json)

不完全确定这里的功能差异是什么”来解决此问题,但这得到了我想要的结果。

I was able to fix this issue by changing

Dim userResult As UserData = TryCast(serializer.DeserializeObject(json), UserData)

to

Dim userResult As UserData = serializer.Deserialize(Of UserData)(json)

Not exactly sure what the functional difference here is, but that got me the result i wanted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文