2017-05-28 140 views
0

我承擔了一個有點困難的任務。我想不過來獲得一些JSON的信息,我不斷收到無法反序列化當前的JSON對象錯誤VB.NET無法反序列化當前的JSON對象

這裏是我的代碼

Public Class Rootobject 
    Public Property TextureAtlas As List(Of Subtexture) 
End Class 

Public Class Textureatlas 
    Public Property SubTexture() As Subtexture 
    Public Property _imagePath As String 
End Class 

Public Class Subtexture 

    Public Property _name As String 
    Public Property _x As String 
    Public Property _y As String 
    Public Property _width As String 
    Public Property _height As String 
End Class 


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim deserialized = JsonConvert.DeserializeObject(Of Rootobject)(RichTextBox1.Text.ToString()) 
    For Each item In deserialized.TextureAtlas 
     MsgBox(item._name) 
    Next 

這裏的錯誤

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll 

    Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[XMLPrasers.Form1+Subtexture]' because the type requires a JSON array (e.g. 

這是JSON

{ 
    "TextureAtlas": { 
     "SubTexture": [ 
     { 
      "_name": "AFKClick", 
      "_x": "0", 
      "_y": "0", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "AFKDef", 
      "_x": "84", 
      "_y": "0", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "AFKHover", 
      "_x": "168", 
      "_y": "0", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "BagClick", 
      "_x": "0", 
      "_y": "84", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "BagDef", 
      "_x": "84", 
      "_y": "84", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "BagHover", 
      "_x": "0", 
      "_y": "168", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "PetClick", 
      "_x": "84", 
      "_y": "168", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "PetDef", 
      "_x": "168", 
      "_y": "84", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "PetHover", 
      "_x": "168", 
      "_y": "168", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "ShopClick", 
      "_x": "252", 
      "_y": "0", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "ShopDef", 
      "_x": "252", 
      "_y": "84", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "ShopHover", 
      "_x": "336", 
      "_y": "0", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "TwitterClick", 
      "_x": "252", 
      "_y": "168", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "TwitterDef", 
      "_x": "336", 
      "_y": "84", 
      "_width": "82", 
      "_height": "82" 
     }, 
     { 
      "_name": "TwitterHover", 
      "_x": "420", 
      "_y": "0", 
      "_width": "82", 
      "_height": "82" 
     } 
     ], 
     "_imagePath": "Button.png" 
    } 
} 
+0

你的課是錯的。如果JSON中有很多這樣的根對象,它們應該只是'Public Property TextureAtlas As TextureAtlas',反序列化爲一個List或者'RootObjects'(壞名稱)數組。另外,'TextureAtlas.SubTexture'應該是一個List或者Array。錯誤消息的其餘部分告訴你有關數組/列表不匹配。 – Plutonix

回答

0

這應該是你的類的結構

Public Class RootObject 
    Public Property TextureAtlas As Textureatlas 
End Class 

Public Class Textureatlas 
    Public Property SubTexture As List(Of subtexture) 
    Public Property _imagePath As String 
End Class 

Public Class Subtexture 

    Public Property _name As String 
    Public Property _x As String 
    Public Property _y As String 
    Public Property _width As String 
    Public Property _height As String 
End Class 
相關問題