【发布时间】:2013-11-15 05:45:12
【问题描述】:
我正在从外部 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": [],
},
}
]
}
我需要从这个 Json 数组中获取 "src": "http://myurl.jpg"element。
我试过了:
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"];
谁能帮帮我?
【问题讨论】:
-
obj 是 JObject 并且媒体是 JArray 。您不能将 JObject 转换为 JArray
-
@MicrosoftDN 你能推荐一个替代方案吗?