反序列化时Json列表为null

时间:2013-11-20 10:36:22

标签: json vb.net list deserialization

我有以下json:

     json = <json>{
"Header": {
    "MCC": "415",
    "FO": "0",
    "REGID": "2"
},
"Contacts": [
    {
        "mo": "70875623",
        "name": "salam"
    },
    {
        "name": "salam2",
        "mo": "70242352"
    }
]
 }</json>.Value

我正试图将这个json分解为一个列表:

Public Class Dataa
Public header As Header
Public Contacts As Contacts()
End Class

<Serializable()> _
Public Class Header
'<System.Runtime.Serialization.DataMember(Name:="MCC")>
Public MCC As Integer
' <System.Runtime.Serialization.DataMember(Name:="FO")>
Public FO As Integer
'<System.Runtime.Serialization.DataMember(Name:="REGID")>
Public RegId As Integer
End Class

<Serializable()> _
Public Class Contacts
Public name As String
Public mo As String
End Class


Dim jss As New JavaScriptSerializer
Dim jsonList As Generic.List(Of Dataa) = jss.Deserialize(Of List(Of Dataa))(json)

问题是我的列表为空...

我在这里错过了一些东西吗?

1 个答案:

答案 0 :(得分:1)

试试这个,我已经将json转换为c#,然后将c#转换为vb.net

VB.NET类..

Public Class Header
    Public Property MCC() As String
        Get
            Return m_MCC
        End Get
        Set
            m_MCC = Value
        End Set
    End Property
    Private m_MCC As String
    Public Property FO() As String
        Get
            Return m_FO
        End Get
        Set
            m_FO = Value
        End Set
    End Property
    Private m_FO As String
    Public Property REGID() As String
        Get
            Return m_REGID
        End Get
        Set
            m_REGID = Value
        End Set
    End Property
    Private m_REGID As String
End Class

Public Class Contact
    Public Property mo() As String
        Get
            Return m_mo
        End Get
        Set
            m_mo = Value
        End Set
    End Property
    Private m_mo As String
    Public Property name() As String
        Get
            Return m_name
        End Get
        Set
            m_name = Value
        End Set
    End Property
    Private m_name As String
End Class

Public Class RootObject
    Public Property Header() As Header
        Get
            Return m_Header
        End Get
        Set
            m_Header = Value
        End Set
    End Property
    Private m_Header As Header
    Public Property Contacts() As List(Of Contact)
        Get
            Return m_Contacts
        End Get
        Set
            m_Contacts = Value
        End Set
    End Property
    Private m_Contacts As List(Of Contact)
End Class

Json数据

{
    "Header": {
        "MCC": "415",
        "FO": "0",
        "REGID": "2"
    },
    "Contacts": [
        {
            "mo": "70875623",
            "name": "salam"
        },
        {
            "name": "salam2",
            "mo": "70242352"
        }
    ]
}

反序列化json:

Dim json = "{'Header': {'MCC': '415','FO': '0','REGID': '2'},'Contacts': [{'mo': '70875623','name': 'salam'},{'name': 'salam2','mo': '70242352'}]}"
Dim jss As New JavaScriptSerializer
Dim jsonList As RootObject = jss.Deserialize(Of RootObject)(json)

enter image description here