1

我是 json 新手,正在努力将一些嵌套的 json 反序列化为一个对象。外部对象工作正常,但我没有得到内部对象的任何值。我尝试了几种解决方案,包括使用列表对象、集合、datacontractserializer,但似乎没有任何效果。我想我可能遗漏了一些明显的东西。这是我现在拥有的:

json 字符串如下所示: {"type":"lookup","message":"Success","version":0.1,"user":{"loginName":"username","vendor":null}}

我的代码如下:

<Serializable()> Public Class LookupReturn
    Private _Type As String = ""
    Private _Message As String = ""
    Private _Version As String = ""
    Private _user As New jsonUser


    Public Property Type() As String
        Get
            Return _Type
        End Get
        Set(ByVal value As String)
            _Type = value
        End Set
    End Property

    Public Property Message() As String
        Get
            Return _Message
        End Get
        Set(ByVal value As String)
            _Message = value
        End Set
    End Property

    Public Property Version() As String
        Get
            Return _Version
        End Get
        Set(ByVal value As String)
            _Version = value
        End Set
    End Property

    Public Property Userobj() As jsonUser
        Get
            Return _user
        End Get
        Set(ByVal value As jsonUser)
            _user = value
        End Set
    End Property

End Class

<Serializable()> Public Class jsonUser
    Private _loginName As String = ""
    Private _vendor As String = ""

    Public Property loginName() As String
        Get
            Return _loginName
        End Get
        Set(ByVal value As String)
            _loginName = value
        End Set
    End Property

    Public Property vendor() As String
        Get
            Return _vendor
        End Get
        Set(ByVal value As String)
            _vendor = value
        End Set
    End Property

End Class

Dim _Json As New JavaScriptSerializer()
Dim _Message as string = "{"type":"lookup","message":"Success","version":0.1,"user"{"loginName":"username","vendor":null}}"
Dim returnData As LookupReturn = _Json.Deserialize(Of LookupReturn)(_Message)

我正在获取 LookupReturn 对象的类型、消息、版本值中的数据,并且它正在为用户项返回一个对象,但 loginName 的值是一个空字符串。

任何帮助,将不胜感激!

谢谢!

4

1 回答 1

0

在处理了更多之后,我意识到问题在于我的嵌套对象没有按照我的代码的 Get/Set 部分中的名称命名。因为我将它称为“Userobj”而不是在 json 中调用的“user”,所以反序列化器无法正常工作。我将名称更改为“用户”,一切正常!

于 2012-09-30T18:08:53.307 回答