我正在从外部 Web 服务获取一些数据并使用Newtonsoft.Json.Linq将其解析为 json
像这样
JObject o = JObject.Parse(json);
JArray sizes = (JArray) o["data"];
现在Sizes
看起来像这样
{
[
{
"post_id": "13334556777742_6456",
"message": "messagecomes her",
"attachment": {
"media": [
{
"href": "http://onurl.html",
"alt": "",
"type": "link",
"src": "http://myurl.jpg"
}
],
"name": "come to my name",
"href": "http://mydeeplink.html",
"description": "",
"properties": [],
},
}
]
}
我需要"src": "http://myurl.jpg"
从此 Json 数组中获取元素。我试过了:
foreach (JObject obj in sizes)
{
JArray media = (JArray)obj["attachment"];
foreach (JObject obj1 in media)
{
var src = obj1["src"];
}
}
但它抛出一个错误:
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'.
在这条线上
JArray media = (JArray)obj["attachment"];
任何人都可以帮我解决这个问题吗?