反序列化最常见的JSON响应

时间:2014-12-24 21:39:50

标签: json vb.net

我尝试从最老的项目中反序列化JSON响应。 http://the.echonest.com/

我确实在字符串中读到了响应:

{  
   "response":{  
      "status":{  
         "version":"4.2",
         "code":0,
         "message":"Success"
      },
      "songs":[  
         {  
            "tag":0,
            "error":"need codes in query for fingerprint matching"
         },
         {  
            "tag":2,
            "score":32,
            "title":"Guilty Filthy Soul",
            "message":"OK (match type 5)",
            "artist_id":"ARILGMH1447548D699",
            "artist_name":"AWOLNATIONMSDeluxeEdition.jpg",
            "id":"SOJIPHL12BF395951C"
         },
         {  
            "tag":1,
            "score":39,
            "title":"Soul Wars",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOJHHWN130516E1917"
         },
         {  
            "tag":3,
            "score":46,
            "title":"MF",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"Awolnation",
            "id":"SODOMSN1360091E842"
         },
         {  
            "tag":12,
            "score":41,
            "title":"Swinging From The Castles",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"Awolnation",
            "id":"SONTNCN1425843249E"
         },
         {  
            "tag":15,
            "score":36,
            "title":"Shoestrings",
            "message":"OK (match type 5)",
            "artist_id":"ARILGMH1447548D699",
            "artist_name":"AWOLNATIONMSDeluxeEdition.jpg",
            "id":"SOLLDKR142585BFA69"
         },
         {  
            "tag":10,
            "score":28,
            "title":"Not Your Fault (Robert DeLong Remix)",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SORWXAJ1421C6F8E56"
         },
         {  
            "tag":4,
            "score":43,
            "title":"Some Kind of Joke",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"Awolnation",
            "id":"SOSFVGK142586E007F"
         },
         {  
            "tag":9,
            "score":37,
            "title":"Sail (Borgore Pop The Sweating I'm Sailing Remix)",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"Awolnation",
            "id":"SOWQQUP14258815CCA"
         },
         {  
            "tag":21,
            "score":56,
            "title":"Sail - Tde Remix Featuring Kendrick Lamar & Ab Soul",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SODVLLQ13F68EE4DB0"
         },
         {  
            "tag":20,
            "score":73,
            "title":"Wake Up",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOJYTIP130516E077F"
         },
         {  
            "tag":18,
            "score":61,
            "title":"Sail - Dan The Automator Remix",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOYJPEA1393D0A8275"
         },
         {  
            "tag":8,
            "score":71,
            "title":"All I Need",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOCTVVU1315CD5159E"
         },
         {  
            "tag":16,
            "score":45,
            "title":"Kill Your Heroes",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOSHUZU1315CD51CC6"
         },
         {  
            "tag":23,
            "score":40,
            "title":"Burn It Down - Innerpartysystem Remix",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOWBQBY12D9F5212E2"
         },
         {  
            "tag":24,
            "score":35,
            "title":"Megalithic Symphony",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOLYFXI1315CD50F08"
         },
         {  
            "tag":17,
            "score":21,
            "title":"Soul Wars - Live in Salzburg, Austria",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SORXTYU142587B6D9B"
         },
         {  
            "tag":14,
            "score":64,
            "title":"Everybody’s Got A Secret",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"Awolnation",
            "id":"SOJBTCM13AA5063525"
         },
         {  
            "tag":26,
            "score":70,
            "title":"People",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"Awolnation",
            "id":"SOGPZIH1425829F571"
         },
         {  
            "tag":5,
            "score":71,
            "title":"Sail",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"Awolnation",
            "id":"SOIBWQG131634AC357"
         },
         {  
            "tag":25,
            "score":62,
            "title":"Sail (Unlimited Gravity Remix)",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOTEBOO1393CF870D4"
         },
         {  
            "tag":27,
            "score":41,
            "title":"Burn It Down",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOZAEHP131634ABE40"
         },
         {  
            "tag":6,
            "score":40,
            "title":"Jump On My Shoulders",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOEQIJG1315CD51AE8"
         },
         {  
            "tag":29,
            "score":33,
            "title":"I've Been Dreaming",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOUEMDH13E96158B43"
         },
         {  
            "tag":19,
            "score":33,
            "title":"Thiskidsnotalright",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOYHQBE13EBDBB87B0"
         },
         {  
            "tag":22,
            "score":72,
            "title":"Guilty Filthy Soul (feat. Walé) - Samantha Ronson Remix",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOFZULB1338A5D71A3"
         },
         {  
            "tag":30,
            "score":47,
            "title":"People",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"Awolnation",
            "id":"SOEHJMA1315CD517AF"
         },
         {  
            "tag":31,
            "score":42,
            "title":"Jump On My Shoulders (Thomas From Ghostland Observatory Remix)",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"Awolnation",
            "id":"SOVIGND142582A368B"
         },
         {  
            "tag":7,
            "score":8,
            "title":"Some Sort of Creature",
            "message":"OK (match type 6)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOCGVUY13670F53A9C"
         },
         {  
            "tag":11,
            "score":103,
            "title":"Knights of Shame",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOIYLVA140C222E546"
         },
         {  
            "tag":28,
            "score":81,
            "title":"Not Your Fault",
            "message":"OK (match type 5)",
            "artist_id":"ARJVYUI12AA0D8C20C",
            "artist_name":"AWOLNATION",
            "id":"SOIZFJM1315CD537B0"
         }
      ]
   }
}

我想通过“ForEach”“挖掘”这个回复。如果我只是将每首歌曲作为列表视图中的项目或其他易于处理的对象使用以下键出现,那将是非常棒的:

    Dim lvResponse As ListView
    lvResponse = New ListView
    lvResponse.Columns.Add("Index")
    lvResponse.Columns.Add("Score")
    lvResponse.Columns.Add("Title")
    lvResponse.Columns.Add("Message")
    lvResponse.Columns.Add("ArtistID")
    lvResponse.Columns.Add("Artist")
    lvResponse.Columns.Add("ID")

但是我被卡住了。如果我将JSON字符串放入这样的字典中,我只能显示调试器中的所有项目:

Dim dict = jss.Deserialize(Of Dictionary(Of String, Object))(JSON)

由于我是初学者,我不知道如何处理字典,也不知道C#。所以纯VB.net的答案真的很有帮助。

正如Plutonix所说,这个JSON是一个“复杂的对象” - >当我尝试询问dict.item的调试器时,我得到了以下内容:

(New System.Collections.Generic.Mscorlib_DictionaryDebugView(Of String, Object)((New System.Collections.Generic.Mscorlib_DictionaryDebugView(Of String, Object)(dict)).Items(0).Value)).Items(0)

第二个:

(New System.Collections.Generic.Mscorlib_DictionaryDebugView(Of String, Object)((New System.Collections.Generic.Mscorlib_DictionaryDebugView(Of String, Object)(dict)).Items(0).Value)).Items(1)

在第二个我得到了:

(New System.Collections.Generic.Mscorlib_DictionaryDebugView(Of String, Object)((New System.Collections.Generic.Mscorlib_DictionaryDebugView(Of String, Object)(dict)).Items(0).Value)).Items(1)

在那里最后是一个有31个项目的arraylist。 (当然仅对此样本有效):

这是我的问题的复杂性 - 我不知道如何通过ForEach进行解析。

1 个答案:

答案 0 :(得分:0)

最后我得到了答案!积分在另一个论坛上发给了一个人(Diablo2000)!

解决方案:  首先创建一些代表JSON中对象的类:

Public Class Rootobject
    Public Property response() As Response
End Class

Public Class Response
    Public Property status() As Status
    Public Property songs() As Song()
End Class

Public Class Status
    Public Property version() As String
    Public Property code() As Integer
    Public Property message() As String
End Class

Public Class Song
    Public Property tag() As Integer
    Public Property [error]() As String
    Public Property score() As Integer
    Public Property title() As String
    Public Property message() As String
    Public Property artist_id() As String
    Public Property artist_name() As String
    Public Property id() As String
End Class


Imports System.Web.Script.Serialization
Dim rootObject As Rootobject = New JavaScriptSerializer().Deserialize(Of Rootobject)(json)

最后走到低谷......

If (rootObject.response.status.code = 0) Then
            For Each song As Song In rootObject.response.songs
                Console.WriteLine(song.title)
            Next
        End If