JSON 使用 Class RootObject 反序列化对象
JSON DeserializeObject with Class RootObject
嘿,我有以下 json 字符串:
{
"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 会导致错误,但我不确定为什么?
Additional information: Cannot apply indexing with [] to an expression of type 'mR.musicJsonReturn.RootObject'
尝试:
Console.WriteLine(data.metadata.music[0].label);
您使用的不是数组,而是对象。
永远不要使用动态。
嘿,我有以下 json 字符串:
{
"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 会导致错误,但我不确定为什么?
Additional information: Cannot apply indexing with [] to an expression of type 'mR.musicJsonReturn.RootObject'
尝试:
Console.WriteLine(data.metadata.music[0].label);
您使用的不是数组,而是对象。
永远不要使用动态。