如何解析Twitter趋势Json响应

时间:2013-04-18 18:41:02

标签: c# json twitter json.net

我正在尝试解析以下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响应以获取每个人的“名字”

1 个答案:

答案 0 :(得分:1)

我能够使用此功能获取名称。

dynamic dynObj = JsonConvert.DeserializeObject(jsonString);

foreach (var trend in dynObj[0].trends)
{
    Console.WriteLine(trend.name);
}

如果您计划获取除名称之外的任何内容,那么为JSON对象编写类可能会更好,但我希望这会有所帮助。