次の JSON を解析して name プロパティにアクセスしようとしています。しかし、私はいくつかの困難を抱えています。
{[
{
"trends": [
{
"name": "#penaltypointsforpricks",
"url": "http://twitter.com/search?q=%23penaltypointsforpricks",
"promoted_content": null,
"query": "%23penaltypointsforpricks",
"events": null
},
{
"name": "#sometimesiwishthat",
"url": "http://twitter.com/search?q=%23sometimesiwishthat",
"promoted_content": null,
"query": "%23sometimesiwishthat",
"events": null
},
{
"name": "#1000daysof1d",
"url": "http://twitter.com/search?q=%231000daysof1d",
"promoted_content": null,
"query": "%231000daysof1d",
"events": null
},
{
"name": "#ireland",
"url": "http://twitter.com/search?q=%23ireland",
"promoted_content": null,
"query": "%23ireland",
"events": null
},
{
"name": "#savita",
"url": "http://twitter.com/search?q=%23savita",
"promoted_content": null,
"query": "%23savita",
"events": null
},
{
"name": "Twitter",
"url": "http://twitter.com/search?q=Twitter",
"promoted_content": null,
"query": "Twitter",
"events": null
},
{
"name": "Cork",
"url": "http://twitter.com/search?q=Cork",
"promoted_content": null,
"query": "Cork",
"events": null
},
{
"name": "Facebook",
"url": "http://twitter.com/search?q=Facebook",
"promoted_content": null,
"query": "Facebook",
"events": null
},
{
"name": "Boston",
"url": "http://twitter.com/search?q=Boston",
"promoted_content": null,
"query": "Boston",
"events": null
},
{
"name": "Will",
"url": "http://twitter.com/search?q=Will",
"promoted_content": null,
"query": "Will",
"events": null
}
],
"as_of": "2013-04-18T18:34:45Z",
"created_at": "2013-04-18T18:29:40Z",
"locations": [
{
"name": "Dublin",
"woeid": 560743
}
]
}
]}
次の提案がありましたが、実行時に null 参照例外が返されます。
var twitterObject = JToken.Parse(jsonString);
var trendsArray = twitterObject.Children<JProperty>().FirstOrDefault(x => x.Name == "trends").Value;
foreach (var item in trendsArray.Children())
{
var itemProperties = item.Children<JProperty>();
//you could do a foreach or a linq here depending on what you need to do exactly with the value
var myElement = itemProperties.FirstOrDefault(x => x.Name == "url");
var myElementValue = myElement.Value; ////This is a JValue type
}
このjson応答を解析して個々の「名前」を取得する方法について、誰でも提案があります