2014-06-26 133 views
1

對於我的生活,我似乎無法弄清楚爲什麼JSON.Net無法將我的JSON數據反序列化爲自定義對象。我懷疑它與我的課程有關。我沒有收到任何錯誤,但是當我在代碼中插入一個斷點並在創建它的行之後檢查該對象時,對象內部沒有任何內容。我已驗證變量strBackupInfo包含JSON數據。無法讓Json.Net反序列化爲自定義對象

以下是我與

{ 
    "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": "[email protected]", 
       "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數據下面是我從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 

回答

0

由於您將JSON反序列化爲錯誤的類,因此您正在獲取空對象。您應該將您的RootObject類反序列化,而不是JSONBackupInfo

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

還要注意,你的問題被標記爲Json.Net,但它似乎您使用的是JavaScriptSerializerSystem.Web.Extensions組件,這是不Json.Net的一部分。根據您的需要,JavaScriptSerializer可能會或可能不會爲您工作,但如果您打算使用Json.Net,則應該使用JsonConvert.DeserializeObject來代替。

Dim BackupObject As RootObject = _ 
      JsonConvert.DeserializeObject(Of RootObject)(strBackupInfo)