如何访问 JArray 的元素(或迭代它们) [英] How to access elements of a JArray (or iterate over them)

查看:88
本文介绍了如何访问 JArray 的元素(或迭代它们)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Twitter 获得了以下 Json

I have the following Json gotten from Twitter

     +      token   {[
  {
    "trends": [
      {
        "name": "Croke Park II",
        "url": "http://twitter.com/search?q=%22Croke+Park+II%22",
        "promoted_content": null,
        "query": "%22Croke+Park+II%22",
        "events": null
      },
      {
        "name": "#twiznight",
        "url": "http://twitter.com/search?q=%23twiznight",
        "promoted_content": null,
        "query": "%23twiznight",
        "events": null
      },
      {
        "name": "#Phanhattan",
        "url": "http://twitter.com/search?q=%23Phanhattan",
        "promoted_content": null,
        "query": "%23Phanhattan",
        "events": null
      },
      {
        "name": "#VinB",
        "url": "http://twitter.com/search?q=%23VinB",
        "promoted_content": null,
        "query": "%23VinB",
        "events": null
      },
      {
        "name": "#Boston",
        "url": "http://twitter.com/search?q=%23Boston",
        "promoted_content": null,
        "query": "%23Boston",
        "events": null
      },
      {
        "name": "#rtept",
        "url": "http://twitter.com/search?q=%23rtept",
        "promoted_content": null,
        "query": "%23rtept",
        "events": null
      },
      {
        "name": "Facebook",
        "url": "http://twitter.com/search?q=Facebook",
        "promoted_content": null,
        "query": "Facebook",
        "events": null
      },
      {
        "name": "Ireland",
        "url": "http://twitter.com/search?q=Ireland",
        "promoted_content": null,
        "query": "Ireland",
        "events": null
      },
      {
        "name": "Everton",
        "url": "http://twitter.com/search?q=Everton",
        "promoted_content": null,
        "query": "Everton",
        "events": null
      },
      {
        "name": "Twitter",
        "url": "http://twitter.com/search?q=Twitter",
        "promoted_content": null,
        "query": "Twitter",
        "events": null
      }
    ],
    "as_of": "2013-04-17T13:05:30Z",
    "created_at": "2013-04-17T12:51:41Z",
    "locations": [
      {
        "name": "Dublin",
        "woeid": 560743
      }
    ]
  }
]}  Newtonsoft.Json.Linq.JToken {Newtonsoft.Json.Linq.JArray}

问题是我似乎无法访问任何元素.我尝试过 foreach 循环和普通的 for 循环,但似乎永远无法访问单个元素,它总是最终访问整个区域.

Problem is I can't seem to access any of the elements. I have tried foreach loops and normal for loops and can never seem to access individual elemets it always ends up accessing the whole area.

知道如何访问这个 Json JArray 中的各个元素吗?

Any idea how I access the individual elements in this Json JArray?

推荐答案

更新 - 我验证了以下工作.也许你的 JArray 的创建不太正确.

Update - I verified the below works. Maybe the creation of your JArray isn't quite right.

[TestMethod]
    public void TestJson()
    {
        var jsonString = @"{""trends"": [
              {
                ""name"": ""Croke Park II"",
                ""url"": ""http://twitter.com/search?q=%22Croke+Park+II%22"",
                ""promoted_content"": null,
                ""query"": ""%22Croke+Park+II%22"",
                ""events"": null
              },
              {
                ""name"": ""Siptu"",
                ""url"": ""http://twitter.com/search?q=Siptu"",
                ""promoted_content"": null,
                ""query"": ""Siptu"",
                ""events"": null
              },
              {
                ""name"": ""#HNCJ"",
                ""url"": ""http://twitter.com/search?q=%23HNCJ"",
                ""promoted_content"": null,
                ""query"": ""%23HNCJ"",
                ""events"": null
              },
              {
                ""name"": ""Boston"",
                ""url"": ""http://twitter.com/search?q=Boston"",
                ""promoted_content"": null,
                ""query"": ""Boston"",
                ""events"": null
              },
              {
                ""name"": ""#prayforboston"",
                ""url"": ""http://twitter.com/search?q=%23prayforboston"",
                ""promoted_content"": null,
                ""query"": ""%23prayforboston"",
                ""events"": null
              },
              {
                ""name"": ""#TheMrsCarterShow"",
                ""url"": ""http://twitter.com/search?q=%23TheMrsCarterShow"",
                ""promoted_content"": null,
                ""query"": ""%23TheMrsCarterShow"",
                ""events"": null
              },
              {
                ""name"": ""#Raw"",
                ""url"": ""http://twitter.com/search?q=%23Raw"",
                ""promoted_content"": null,
                ""query"": ""%23Raw"",
                ""events"": null
              },
              {
                ""name"": ""Iran"",
                ""url"": ""http://twitter.com/search?q=Iran"",
                ""promoted_content"": null,
                ""query"": ""Iran"",
                ""events"": null
              },
              {
                ""name"": ""#gaa"",
                ""url"": ""http://twitter.com/search?q=%23gaa"",
                ""promoted_content"": null,
                ""query"": ""gaa"",
                ""events"": null
              },
              {
                ""name"": ""Facebook"",
                ""url"": ""http://twitter.com/search?q=Facebook"",
                ""promoted_content"": null,
                ""query"": ""Facebook"",
                ""events"": 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
        }
    }

因此调用 JArray 上的 Children 以获取 JArray 中的每个 JObject.在每个 JObject 上调用 Children 以访问对象属性.

So call Children on your JArray to get each JObject in JArray. Call Children on each JObject to access the objects properties.

foreach(var item in yourJArray.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
}

这篇关于如何访问 JArray 的元素(或迭代它们)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆