【问题标题】:Extracting elements wherever JArray in json在json中的JArray中提取元素
【发布时间】:2015-11-18 14:03:23
【问题描述】:

让我们看看,json 可以是动态的,并且可能在任何属性中都可以包含多个嵌套数组。

例子:

{
    "items": [
        {
            "id": "0001",
            "name": "Cake",
            "batters": {
                "batter": [
                    {
                        "id": "1001",
                        "type": "Regular"
                    },
                    {
                        "id": "1002",
                        "type": "Chocolate"
                    },
                    {
                        "dry": [
                            {
                                "id": "1003",
                                "type": "Devil's Food"
                            }
                        ]
                    }
                ],
                "other": [
                    {
                        "id": "1004",
                        "type": "Home Food"
                    }
                ]
            },
            "topping": [
                {
                    "id": "5002",
                    "type": "Glazed"
                },
                {
                    "id": "5005",
                    "type": "Sugar"
                }
            ]
        },
        {
            "id": "0002",
            "name": "Sweets"
        }
    ]
}

一个简单的列表应该返回如下元素:

[
    {
        "id": "1001",
        "type": "Regular"
    },
    {
        "id": "1002",
        "type": "Chocolate"
    },
    {
        "id": "1003",
        "type": "Devil's Food"
    },
    {
        "id": "1004",
        "type": "Home Food"
    },
    {
        "id": "5002",
        "type": "Glazed"
    },
    {
        "id": "5005",
        "type": "Sugar"
    },
    {
        "id": "0002",
        "name": "Sweets"
    }
]

请注意: Json 什么都可以,没有属性可以用来提取,只要知道需要的是 JArray 里面的东西。

到目前为止我已经尝试过,但这只是一个开始:

public static bool ParseJsonArray(JToken token, List<string> extracts, string parentLocation = "")
        {
            if (token.HasValues)
            {
                foreach (JToken child in token.Children())
                {
                    if (token.Type == JTokenType.Array)
                    {
                        parentLocation += ((JProperty)token).Name;
                        extracts.Add(token.ToString());
                    }
                    ParseJsonArray(child, extracts, parentLocation);
                }
                return true;
            }
            else
            {
                return false;
            }
        }

token这里是解析后的动态json。

【问题讨论】:

  • 动态数据 = Json.Decode(json); - 这对你有用吗?
  • { "id": "0001", "name": "Cake" } 怎么样?这应该包含在列表中吗?

标签: c# arrays json json.net


【解决方案1】:

您似乎想递归地查找所有本身不包含嵌套数组的 JArray 条目。我们称这些“叶子”数组条目。我这么说是因为您没有在结果中包含以下非叶子条目:

    {
        "id": "0001",
        "name": "Cake"
    }

话虽如此,您可以使用以下扩展方法找到叶数组条目:

public static class JsonExtensions
{
    public static IEnumerable<JToken> LeafArrayEntries(this JContainer container)
    {
        var nonLeafEntries = new HashSet<JToken>(container.DescendantsAndSelf()
            .OfType<JArray>()
            .SelectMany(a => a.Ancestors().Where(p => p.Type != JTokenType.Property)));
        return container.DescendantsAndSelf().Where(c => c.Parent is JArray && !nonLeafEntries.Contains(c));
    }
}

然后将返回的项目放入自己的数组中:

var leafItemArray = new JArray(rootJContainer.LeafArrayEntries());

【讨论】:

  • 谢谢哥们,它可以满足我的需要。您可以编辑您的答案以包括{ "id": "0001", "name": "Cake" } 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
相关资源
最近更新 更多