0

我正在尝试解析以下 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 响应以获取每个单独的“名称”有任何建议

4

1 回答 1

1

我能够使用它来获取名称。

dynamic dynObj = JsonConvert.DeserializeObject(jsonString);

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

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

于 2013-04-18T19:53:17.077 回答