无法让Json.Net反序列化为自定义对象

时间:2014-06-26 20:50:02

标签: visual-studio-2013 json.net

对于我的生活,我似乎无法弄清楚为什么JSON.Net无法将我的JSON数据反序列化为自定义对象。我怀疑它与我的班级有关。我没有收到任何错误,但是当我在代码中放置并在创建它的行之后检查对象时,对象内部没有任何内容。我已经验证变量strBackupInfo确实包含JSON数据。

以下是我正在使用的JSON数据

{
    "metadata": {
        "timestamp": "2014-06-26T16:42:40.041-04:00",
        "params": {
            "username": "test-account",
            "incComputerCount": "true"
        }
    },
    "data": {
        "totalCount": 1,
        "users": [
            {
                "userId": 5,
                "userUid": "7f637e0e9b5a7909",
                "status": "Active",
                "username": "test-account",
                "email": "test-account@corp.net",
                "firstName": "",
                "lastName": "",
                "quotaInBytes": -1,
                "orgId": 3,
                "orgUid": "551317080886150419",
                "orgName": "MiWorkspace",
                "active": true,
                "blocked": false,
                "emailPromo": true,
                "invited": false,
                "orgType": "ENTERPRISE",
                "usernameIsAnEmail": false,
                "creationDate": "2012-10-15T13:13:51.353-04:00",
                "modificationDate": "2014-03-05T15:08:38.637-05:00",
                "passwordReset": false,
                "computerCount": 3
            }
        ]
    }
}

以下是我根据JSON数据创建的自定义类:

Public Class JSONBackupInfo
    Public Property username() As String
        Get
            Return m_username
        End Get
        Set(value As String)
            m_username = value
        End Set
    End Property
    Private m_username As String
    Public Property incComputerCount() As String
        Get
            Return m_incComputerCount
        End Get
        Set(value As String)
            m_incComputerCount = value
        End Set
    End Property
    Private m_incComputerCount As String
End Class

Public Class Metadata
    Public Property timestamp() As String
        Get
            Return m_timestamp
        End Get
        Set(value As String)
            m_timestamp = Value
        End Set
    End Property
    Private m_timestamp As String
    Public Property params() As JSONBackupInfo

        Get
            Return m_params
        End Get
        Set(value As JSONBackupInfo)
            m_params = value
        End Set
    End Property
    Private m_params As JSONBackupInfo
End Class

Public Class User
    Public Property userId() As Integer
        Get
            Return m_userId
        End Get
        Set(value As Integer)
            m_userId = Value
        End Set
    End Property
    Private m_userId As Integer
    Public Property userUid() As String
        Get
            Return m_userUid
        End Get
        Set(value As String)
            m_userUid = Value
        End Set
    End Property
    Private m_userUid As String
    Public Property status() As String
        Get
            Return m_status
        End Get
        Set(value As String)
            m_status = Value
        End Set
    End Property
    Private m_status As String
    Public Property username() As String
        Get
            Return m_username
        End Get
        Set(value As String)
            m_username = Value
        End Set
    End Property
    Private m_username As String
    Public Property email() As String
        Get
            Return m_email
        End Get
        Set(value As String)
            m_email = Value
        End Set
    End Property
    Private m_email As String
    Public Property firstName() As String
        Get
            Return m_firstName
        End Get
        Set(value As String)
            m_firstName = Value
        End Set
    End Property
    Private m_firstName As String
    Public Property lastName() As String
        Get
            Return m_lastName
        End Get
        Set(value As String)
            m_lastName = Value
        End Set
    End Property
    Private m_lastName As String
    Public Property quotaInBytes() As Integer
        Get
            Return m_quotaInBytes
        End Get
        Set(value As Integer)
            m_quotaInBytes = Value
        End Set
    End Property
    Private m_quotaInBytes As Integer
    Public Property orgId() As Integer
        Get
            Return m_orgId
        End Get
        Set(value As Integer)
            m_orgId = Value
        End Set
    End Property
    Private m_orgId As Integer
    Public Property orgUid() As String
        Get
            Return m_orgUid
        End Get
        Set(value As String)
            m_orgUid = Value
        End Set
    End Property
    Private m_orgUid As String
    Public Property orgName() As String
        Get
            Return m_orgName
        End Get
        Set(value As String)
            m_orgName = Value
        End Set
    End Property
    Private m_orgName As String
    Public Property active() As Boolean
        Get
            Return m_active
        End Get
        Set(value As Boolean)
            m_active = Value
        End Set
    End Property
    Private m_active As Boolean
    Public Property blocked() As Boolean
        Get
            Return m_blocked
        End Get
        Set(value As Boolean)
            m_blocked = Value
        End Set
    End Property
    Private m_blocked As Boolean
    Public Property emailPromo() As Boolean
        Get
            Return m_emailPromo
        End Get
        Set(value As Boolean)
            m_emailPromo = Value
        End Set
    End Property
    Private m_emailPromo As Boolean
    Public Property invited() As Boolean
        Get
            Return m_invited
        End Get
        Set(value As Boolean)
            m_invited = Value
        End Set
    End Property
    Private m_invited As Boolean
    Public Property orgType() As String
        Get
            Return m_orgType
        End Get
        Set(value As String)
            m_orgType = Value
        End Set
    End Property
    Private m_orgType As String
    Public Property usernameIsAnEmail() As Boolean
        Get
            Return m_usernameIsAnEmail
        End Get
        Set(value As Boolean)
            m_usernameIsAnEmail = Value
        End Set
    End Property
    Private m_usernameIsAnEmail As Boolean
    Public Property creationDate() As String
        Get
            Return m_creationDate
        End Get
        Set(value As String)
            m_creationDate = Value
        End Set
    End Property
    Private m_creationDate As String
    Public Property modificationDate() As String
        Get
            Return m_modificationDate
        End Get
        Set(value As String)
            m_modificationDate = Value
        End Set
    End Property
    Private m_modificationDate As String
    Public Property passwordReset() As Boolean
        Get
            Return m_passwordReset
        End Get
        Set(value As Boolean)
            m_passwordReset = Value
        End Set
    End Property
    Private m_passwordReset As Boolean
    Public Property computerCount() As Integer
        Get
            Return m_computerCount
        End Get
        Set(value As Integer)
            m_computerCount = Value
        End Set
    End Property
    Private m_computerCount As Integer
End Class

Public Class Data
    Public Property totalCount() As Integer
        Get
            Return m_totalCount
        End Geta
        Set(value As Integer)
            m_totalCount = Value
        End Set
    End Property
    Private m_totalCount As Integer
    Public Property users() As List(Of User)
        Get
            Return m_users
        End Get
        Set(value As List(Of User))
            m_users = Value
        End Set
    End Property
    Private m_users As List(Of User)
End Class

Public Class RootObject
    Public Property metadata() As Metadata
        Get
            Return m_metadata
        End Get
        Set(value As Metadata)
            m_metadata = Value
        End Set
    End Property
    Private m_metadata As Metadata
    Public Property data() As Data
        Get
            Return m_data
        End Get
        Set(value As Data)
            m_data = Value
        End Set
    End Property
    Private m_data As Data
End Class

这是我的代码:

Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Dim strBackupInfo As String


        Try
            ' Create the web request 
            For Each strUser As String In txtBackupUsers.Text.Split(vbNewLine)

                request = DirectCast(WebRequest.Create("UR"), HttpWebRequest)

                ' Add authentication to request  
                request.Credentials = New NetworkCredential(HIDDEN)

                ' Get response  
                response = DirectCast(request.GetResponse(), HttpWebResponse)

                ' Get the response stream into a reader  
                reader = New StreamReader(response.GetResponseStream())

                ' Console application output  
                strBackupInfo = reader.ReadToEnd().ToString
                txtBackupResults.Text = strBackupInfo


                Dim BackupObject As JSONBackupInfo = New JavaScriptSerializer().Deserialize(Of JSONBackupInfo)(strBackupInfo)

            Next
        Finally
            If Not response Is Nothing Then response.Close()
        End Try

1 个答案:

答案 0 :(得分:0)

您正在获取一个空对象,因为您正在将JSON反序列化为错误的类。您应该反序列化到RootObject课程,而不是JSONBackupInfo

Dim BackupObject As RootObject = _
          New JavaScriptSerializer().Deserialize(Of RootObject)(strBackupInfo)

另请注意,您的问题被标记为Json.Net,但您似乎正在使用JavaScriptSerializer程序集中的System.Web.Extensions,而JsonConvert.DeserializeObject程序集不属于Json.Net。根据您的需要,JavaScriptSerializer可能适用于您,也可能不适用,但如果您打算使用Json.Net,那么您应该使用Dim BackupObject As RootObject = _ JsonConvert.DeserializeObject(Of RootObject)(strBackupInfo) 代替。

{{1}}