【问题标题】:Dictionary<string, JToken> recursive searchDictionary<string, JToken> 递归搜索
【发布时间】:2019-04-11 14:58:30
【问题描述】:

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

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:

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

如何使用 type ="row" 和索引值(索引 1 的增量以评估下一行)属性来获取使用我的 Dictionary 中列对象的 parentId 的所有列。

期望的输出:

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

【问题讨论】:

  • 我确定我已经尝试过这种方式,但它不起作用。我可以通过 rootobject = JsonConvert.DeserializeObject(json) 访问所有字典。 rootobject.table.item 但我想过滤它,因为有很多行/列关系,因为有很多行对象具有多列。

标签: c# json linq json.net


【解决方案1】:

您可以使用 linq 查找您的根对象

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 并从字典中获取所有项目:

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

附:您输入的 json 无效,缺少几个逗号

【讨论】:

  • 啊我不知道Linq可以用来访问属性!这只会访问索引 0,我如何创建一个循环,以便它递增索引,直到找到所有行和相关列,因为在无序 json 列表中可以有索引 x。会在while循环中添加代码并查看属性索引吗?
  • @JustMeCOdex 您可以将搜索根元素放入循环中,并将搜索查询修改为x["index"].Value&lt;int&gt;() == i,其中i 将在循环中递增。如果此评论没有帮助,我认为您应该创建一个 new question 并为您提供更多行、当前代码和预期输出的 JSON
猜你喜欢
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-01
  • 2012-09-21
相关资源
最近更新 更多