C# 在JSON中,如何使用C实现将相同的键值对递归添加到现有的一个键上#

C# 在JSON中,如何使用C实现将相同的键值对递归添加到现有的一个键上#,c#,json,recursion,data-structures,C#,Json,Recursion,Data Structures,我一直在尝试不同的方法来实现这一点, 我的意见是: string statment = "(Entity.CIFNumber <> '3123' AND Entity.Country LIKE 'USA' AND (Entity.FYEMonth= 'May' OR Statement.ProgitBeforeTax= '123123' OR STATEMENT.NetSales <= 234234 OR STATEMENT.statementdatekey_ = '2019/

我一直在尝试不同的方法来实现这一点, 我的意见是:

string statment = "(Entity.CIFNumber <> '3123' AND Entity.Country LIKE 'USA' AND (Entity.FYEMonth= 'May' OR Statement.ProgitBeforeTax= '123123' OR STATEMENT.NetSales <= 234234 OR STATEMENT.statementdatekey_ = '2019/07/01'))";
string station=“(Entity.CIFNumber'3123'和Entity.Country,如'USA'和(Entity.FYEMonth='May'或Statement.ProgitBeforeTax='123123'或Statement.NetSales=“,
“价值”:123123
},
{
“字段”:“STATEMENT.NetSales”,
“条件”:“
公共类基
{
}
公共类操作:基本类
{
公共字符串@运算符{get;set;}
公共列表规则{get;set;}
}
公共类规则:基
{
公共字符串字段{get;set;}
公共字符串条件{get;set;}
公共对象值{get;set;}
}
操作op=新操作
{
@operator=“AND”,
规则=新列表
{
新规则{field=“ENTITY.CIFNumber”,condition=“”,value=“3123”},
新规则{field=“ENTITY.Country”,condition=“LIKE”,value=“USA”},
新操作{@operator=“OR”,规则=新列表
{
新规则{field=“ENTITY.FYEMonth”,condition=“=”,value=“May”},
新规则{field=“STATEMENT.ProfitBeforeTax”,条件=“>=”,值=123123},
新规则{field=“STATEMENT.NetSales”,条件=“=”,
“价值”:123123
},
{
“字段”:“STATEMENT.NetSales”,

“条件”:非常感谢您的提示。我按照您的建议创建了类。当我遇到子查询时,我递归调用了相同的函数,该函数在现有规则数组本身下添加了一组新的字段、条件和值。同样,我也面临着同样的问题。很抱歉,我不理解这个问题,您能澄清一下吗你的意思是,当我遇到子查询时,我会递归调用同一个函数,该函数会在现有规则数组本身下添加新的字段、条件和值集?很抱歉,当我的代码遇到字段ENTITY.FYEMonth(在子查询中指代我问题中的输入)时,会出现混淆.ENTITY.FYEMonth将被附加到现有的键规则[]。您可以在回答中看到响应的ENTITY.FYEMonth位于规则[{rules[ENTITY.FYEMonth]}中,这是正确的。对我来说,FYEMonth仍在一级规则列表下。类似于规则[ENTITY.CIFNumber,ENTITY.Country,ENTITY.FYEMonth]我的问题更像是如何递归使用基类。我知道我在那里犯了一些错误。我猜
 "DataSetCommonQuery": {
      "operator": "AND",
      "rules": [
        {
          "field": "ENTITY.CIFNumber",
          "condition": "<>",
          "value": "3123"
        },
        {
          "field": "ENTITY.Country",
          "condition": "LIKE",
          "value": "USA"
        },
        {
          "operator": "OR",
          "rules": [
            {
              "field": "ENTITY.FYEMonth",
              "condition": "=",
              "value": "May"
            },
            {
              "field": "STATEMENT.ProfitBeforeTax",
              "condition": ">=",
              "value": 123123
            },
            {
              "field": "STATEMENT.NetSales",
              "condition": "<=",
              "value": 234234
            },
            {
              "field": "STATEMENT.statementdatekey_",
              "condition": "=",
              "value": "2019-07-01 12:00:00"
            }
          ]
        }
      ]
    },
public class DataSetCommonQuery
    {
        public string @operator { get; set; }
        public List<Rule> rules = new List<Rule>(); 
    }
    public class Rule2
    {
        public string field { get; set; }
        public string condition { get; set; }
        public string value { get; set; }
    }

    public class Rule
    {
        public string field { get; set; }
        public string condition { get; set; }
        public string value { get; set; }
        public string @operator { get; set; }
        public List<Rule2> rules = new List<Rule2>(); //{ get; set; }
    }
public class Base
{

}

public class Operation : Base
{
    public string @operator { get; set; }
    public List<Base> rules { get; set; }
}

public class Rule : Base
{
    public string field { get; set; }
    public string condition { get; set; }
    public object value { get; set; }
}

Operation op = new Operation
{
    @operator = "AND",
    rules = new List<Base>
    {
        new Rule { field = "ENTITY.CIFNumber", condition = "<>", value = "3123" },
        new Rule { field = "ENTITY.Country", condition = "LIKE", value = "USA" },
        new Operation { @operator = "OR", rules = new List<Base>
                                                    {
                                                        new Rule { field = "ENTITY.FYEMonth", condition = "=", value = "May" },
                                                        new Rule { field = "STATEMENT.ProfitBeforeTax", condition = ">=", value = 123123 },
                                                        new Rule { field = "STATEMENT.NetSales", condition = "<=", value = 234234 },
                                                        new Rule { field = "STATEMENT.statementdatekey_", condition = "=", value = new DateTime(2019, 7, 1, 12, 0, 0) }
                                                    }
                      }
    }
};

string json = JsonConvert.SerializeObject(op, Formatting.Indented);
{
  "operator": "AND",
  "rules": [
    {
      "field": "ENTITY.CIFNumber",
      "condition": "<>",
      "value": "3123"
    },
    {
      "field": "ENTITY.Country",
      "condition": "LIKE",
      "value": "USA"
    },
    {
      "operator": "OR",
      "rules": [
        {
          "field": "ENTITY.FYEMonth",
          "condition": "=",
          "value": "May"
        },
        {
          "field": "STATEMENT.ProfitBeforeTax",
          "condition": ">=",
          "value": 123123
        },
        {
          "field": "STATEMENT.NetSales",
          "condition": "<=",
          "value": 234234
        },
        {
          "field": "STATEMENT.statementdatekey_",
          "condition": "=",
          "value": "2019-07-01T12:00:00"
        }
      ]
    }
  ]
}