字典< string,JToken& gt;递归搜索 [英] Dictionary<string, JToken> recursive search

查看:53
本文介绍了字典< string,JToken& gt;递归搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有DeserializeObject到C#对象,但是我有带有动态对象名称的对象,因此我将其结构如下:

I have DeserializeObject to a C# object however I have objects with dynamic object names so I have structured it like this:

public class RootObject
{
    public string name { get; set; }
    public TableLayout table{ get; set; }
}

public class TableLayout 
{
    public Attributes attributes { get; set; } //Static
    public Info info { get; set; } //Static
    [JsonExtensionData]
    public Dictionary<string, JToken> item { get; set; }
}

因此,基本上所有出现的动态对象都将添加到字典中,并且使用JsonExtensionData将填充属性的其余部分,而无需创建对象类.这是我的json:

So basically any dynamic objects that appear will be added to the dictionary and using JsonExtensionData will populate the rest of the property without creating the object classes. Here is my json:

string json = @"
            {
            "name": "Table 100",
            "table": {
                "attributes ": {
                    "id": "attributes",
                    "type": "attributes"
                },
                "info": {
                    "id": "info",
                    "type": "info"
                },
                "item-id12": {
                    "id": "item-id12",
                    "type": "Row"
                    "index": 0
                },
                "item-id16": {
                    "id": "item-id16",
                    "type": "Column"
                    "parentid": "item-id12"
                },
                "item-id21": {
                    "id": "item-id21",
                    "type": "Column",
                    "parentid": "item-id12"
                }
            }
        }";

如何使用类型="row"和索引值(对索引1进行递增来评估下一行)属性使用我的字典中列对象的parentId获取所有列.

How can I use type ="row" and index value(increments to index 1 to evaluate next row) property to get all columns using parentId of column objects in my Dictionary.

所需的输出:

         "item-id12": {
                    "id": "item-id12",
                    "type": "Row"
                    "index": 0
                },
                "item-id16": {
                    "id": "item-id16",
                    "type": "Column"
                    "parentid": "item-id12"
                },
                "item-id21": {
                    "id": "item-id21",
                    "type": "Column",
                    "parentid": "item-id12"
                }

推荐答案

您可以使用linq查找根对象

You can use linq to find your root object

var rootNode = json.table.item.Values
      .FirstOrDefault(x => x["type"].Value<string>() == "Row" && x["index"].Value<int>() == 0);

if (rootNode == null)
    return; // no such item

现在,如果该项目存在,请再次使用linq并从字典中获取所有项目:

Now if this item exists use linq again and get all items from dictionary:

var childNodes = json.table.item.Values
      .Where(x => x["parentid"]?.Value<string>() == rootNode["id"].Value<string>());

下一个代码

var output = new[] {rootNode}.Concat(childNodes);
foreach (var item in output)
    Console.WriteLine(item);

将打印

{
  "id": "item-id12",
  "type": "Row",
  "index": 0
}
{
  "id": "item-id16",
  "type": "Column",
  "parentid": "item-id12"
}
{
  "id": "item-id21",
  "type": "Column",
  "parentid": "item-id12"
}

P.S.您输入的json无效,缺少几个逗号

P.S. Your input json is invalid, it missing few commas

这篇关于字典&lt; string,JToken&amp; gt;递归搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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