2016-08-18 65 views
0

嘿所有我有以下的JSON字符串:JSON DeserializeObject與類RootObject

{ 
    "status":{ 
     "msg":"Success", 
     "code":0, 
     "version":"1.0" 
    }, 
    "metadata":{ 
     "music":[ 
     { 
      "external_ids":{ }, 
      "label":"Atlantic Records", 
      "release_date":"2010-09-13", 
      "album":{ 
       "name":"Passion, Pain & Pleasure" 
      }, 
      "title":"Bottoms Up", 
      "duration_ms":"242013", 
      "genres":[ 
       { 
        "name":"R&B\\Soul\\Funk" 
       } 
      ], 
      "acrid":"63b14329c3beafe35cf08b144a2b4a31", 
      "result_from":3, 
      "artists":[ 
       { 
        "name":"Trey Songz" 
       } 
      ] 
     } 
     ], 
     "timestamp_utc":"2016-08-18 13:56:40" 
    }, 
    "result_type":3 
} 

那我想獲得以下屬性:

label 
album > name 
title 
duration_ms 
genres > name 
artists > name 

的C#代碼,我擁有的是:

dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject<mReconize.musicJsonReturn.RootObject>(json); 
Console.WriteLine(data["metadata"]["music"].label); 

當然data [「metadata」] [「music」]。label is ca使用錯誤,但我不知道爲什麼?

其他信息:不能申請用[]索引到類型的表達式 'mR.musicJsonReturn.RootObject'

enter image description here

+1

嘗試'data.metadata.music [0] .label' – DavidG

+0

謹慎分享爲什麼選擇投票??? – StealthRT

+0

不是我,但也許是因爲它很明顯如何從截圖訪問值? – DavidG

回答

3

嘗試:

Console.WriteLine(data.metadata.music[0].label); 

你不」 t與數組一起工作,但是是一個對象。

永遠不會使用動態。

+1

「永不」使用動態?有很多時候它非常有用。 – DavidG

+0

有幾種情況,當你必須使用動態.... – Fabian

+0

正是,所以你爲什麼說「從來沒有」? – DavidG

相關問題