首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反序列化echonest响应

反序列化echonest响应
EN

Stack Overflow用户
提问于 2014-12-24 21:39:50
回答 1查看 184关注 0票数 0

我试图反序列化来自ec诚实项目的JSON响应。http://the.echonest.com/

我确实在一个字符串中读到了响应,如下所示:

代码语言:javascript
复制
{  
   "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“来”挖掘“这个回复。如果我只是把每首歌作为列表视图中的一个条目或其他易于处理的对象,使用下面的键来呈现,那就太棒了:

代码语言:javascript
复制
    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字符串放入这样的字典中,我才能显示调试器中的所有项:

代码语言:javascript
复制
Dim dict = jss.Deserialize(Of Dictionary(Of String, Object))(JSON)

因为我是初学者,所以我不知道如何处理字典,也不知道C#。因此,纯VB.net的答案将是非常有用的。

正如Plutonix所声明的那样,这个JSON是一个“复杂对象”->,当我试图询问dict.item的调试器时,我得到了以下信息:

代码语言:javascript
复制
(New System.Collections.Generic.Mscorlib_DictionaryDebugView(Of String, Object)((New System.Collections.Generic.Mscorlib_DictionaryDebugView(Of String, Object)(dict)).Items(0).Value)).Items(0)

第二项是:

代码语言:javascript
复制
(New System.Collections.Generic.Mscorlib_DictionaryDebugView(Of String, Object)((New System.Collections.Generic.Mscorlib_DictionaryDebugView(Of String, Object)(dict)).Items(0).Value)).Items(1)

在第二次,我得到了:

代码语言:javascript
复制
(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件物品的清单。(当然只适用于本样本):

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-27 13:17:18

终于找到答案了!学分属于另一个论坛中的一个人(Diablo2000)!

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

代码语言:javascript
复制
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)

最后走到谷底..。

代码语言:javascript
复制
If (rootObject.response.status.code = 0) Then
            For Each song As Song In rootObject.response.songs
                Console.WriteLine(song.title)
            Next
        End If
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27642381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档