如何访问 JArray 的元素(或迭代它们)

Posted

技术标签:

【中文标题】如何访问 JArray 的元素(或迭代它们)【英文标题】:How to access elements of a JArray (or iterate over them) 【发布时间】:2013-04-09 08:40:10 【问题描述】:

我有以下从 Twitter 获得的 Json

     +      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 循环,但似乎永远无法访问单个元素,它总是最终访问整个区域。

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

【问题讨论】:

【参考方案1】:

对此有一个更简单的解决方案。 只需将JArray 的项目视为JObject

假设我们有这样的 JSON 对象数组:

JArray jArray = JArray.Parse(@"[
              
                ""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
              ]");

要访问每个项目,只需执行以下操作:

foreach (JObject item in jArray) // <-- Note that here we used JObject instead of usual JProperty

    string name = item.GetValue("name").ToString();
    string url = item.GetValue("url").ToString();
    // ...

【讨论】:

这实际上是我在 3 个帖子中看到的最直接的答案 @deeps,可能是您传递给 GetValue() 的参数不正确。您能否为您的案例提供更多详细信息? @Just Shadow 谢谢,这是我的错。现在可以正常使用了。【参考方案2】:

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

[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 以访问对象属性。

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

【讨论】:

我想我对你所说的有大致的了解。但是,如果我运行此代码,我最终会获得对 myElementValue 的空引用。或者,如果我在 myElement 上执行 foreach 循环,则 json 结构保持不变,并且我仍然面临多个元素。就好像数组本身格式错误或其他什么,因为它拒绝被迭代。 查看我更新的问题,准确显示我将响应解析为 JArray 时得到的结果 尝试将 json 字符串响应解析为 JToken 而不是 JArray。 现在更新了 JToken 解析的输出。与您的字符串不太匹配【参考方案3】:

一旦你有了一个 JArray,你就可以像对待任何其他 Enumerable 对象一样对待它, 并且使用 linq 你可以访问它们、检查它们、验证它们并选择它们。

var str = @"[1, 2, 3]";
var jArray = JArray.Parse(str);
Console.WriteLine(String.Join("-", jArray.Where(i => (int)i > 1).Select(i => i.ToString())));

【讨论】:

【参考方案4】:
void ConvertToJArray(object headers)

    //where headers is your Twitter response
    JObject JObj = (JObject)headers;//Parse to JObject
    JArray JArr = (JArray)JObj["trends"];//Get the trends array as JArray
    for (int i = 0; i < JArr.Count; i++)
    
        var Name = JArr[i]["name"].ToString();
        var URL = JArr[i]["url"].ToString();
    

【讨论】:

虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。

以上是关于如何访问 JArray 的元素(或迭代它们)的主要内容,如果未能解决你的问题,请参考以下文章

如何访问 for 循环中的上一个/下一个元素?

访问 VBA - 遍历报表元素/属性

使用对 std::vector 的某些元素的引用,或这些元素在向量中的位置来迭代它们

从jarray中删除指定元素的问题

3-22函数进阶——迭代器

使用无效键值访问的 JArray 值:“字段”。预期数组位置索引