Dictionary<string, JToken> recursive search

帅比萌擦擦* 提交于 2019-12-11 09:01:48

问题


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; }
}

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"
                }
            }
        }";

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.

Desired Output:

         "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"
                }

回答1:


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

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>());

Next code

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

will print

{
  "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. Your input json is invalid, it missing few commas



来源:https://stackoverflow.com/questions/55635592/dictionarystring-jtoken-recursive-search

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!