外部 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"];
誰でもこれに手を貸してもらえますか?