如何解析Twitter Trends Json响应



我试图解析以下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
      }
    ]
  }
]}

我有以下建议,但它在运行时返回一个空引用异常。

 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响应,以获得每个单独的"名称"

我可以使用这个获得名称。

dynamic dynObj = JsonConvert.DeserializeObject(jsonString);
foreach (var trend in dynObj[0].trends)
{
    Console.WriteLine(trend.name);
}

如果您打算获取比名称更多的东西,那么为JSON对象编写类可能会更好,但我希望这对您有所帮助。

相关内容

  • 没有找到相关文章

最新更新