You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JSON中合理表示过滤条件的逻辑与关系运算?

解决方案

针对你的需求,推荐采用节点化分层结构,将逻辑组合与具体过滤条件分离,同时统一实体过滤的模型,解决扩展性问题。结合你的SQL背景,这个结构类似SQL的表达式树,理解成本低,且易于扩展。

现有方案的核心问题

  • 逻辑运算符(and/or)与条件对象混合在数组中,结构不直观,解析时需要判断元素类型,增加复杂度
  • 单实体(Live/History)和跨实体(LiveAndHistory)的条件字段不一致(如property vs LiveProperty/HistoryProperty),新增实体时需要修改字段结构,扩展性差
  • 顶级字段按实体类型拆分(Live/History/LiveAndHistory),实体类型越多,JSON结构越臃肿

替代方案设计思路

采用双节点模型,将过滤规则拆分为两种类型的节点:

  1. 逻辑节点:仅负责逻辑组合(AND/OR/NOT),包含操作符和子节点列表,对应SQL中的括号分组逻辑
  2. 条件节点:负责具体的过滤规则,根据实体范围分为单实体条件和跨实体条件,统一字段模型

同时用EntityScope字段明确每个过滤器的适用实体范围,避免顶级字段分散。

示例JSON

{
  "Criteria": {
    "Name": "test Filter",
    "Type": "regular"
  },
  "Filters": [
    {
      "EntityScope": "Live",
      "RootNode": {
        "NodeType": "Logical",
        "Operator": "OR",
        "Children": [
          {
            "NodeType": "Logical",
            "Operator": "AND",
            "Children": [
              {
                "NodeType": "Condition",
                "Property": "HCPCS",
                "Operator": "Is",
                "RelationalOperator": "[]",
                "ValueType": "Simple",
                "Value": ["00100", "01999"]
              },
              {
                "NodeType": "Condition",
                "Property": "RelativeWeight",
                "Operator": "Is",
                "RelationalOperator": "[]",
                "ValueType": "Simple",
                "Value": [1.5, 3.5]
              }
            ]
          },
          {
            "NodeType": "Condition",
            "Property": "Charges",
            "Operator": "Is not",
            "RelationalOperator": ">=",
            "ValueType": "Simple",
            "Value": "5000"
          }
        ]
      }
    },
    {
      "EntityScope": "History",
      "RootNode": {
        "NodeType": "Logical",
        "Operator": "AND",
        "Children": [
          {
            "NodeType": "Condition",
            "Property": "HCPCS",
            "Operator": "Is",
            "RelationalOperator": "[]",
            "ValueType": "Simple",
            "Value": ["00100", "01999"]
          },
          {
            "NodeType": "Condition",
            "Property": "RelativeWeight",
            "Operator": "Is",
            "RelationalOperator": "[]",
            "ValueType": "Simple",
            "Value": [1.5, 3.5]
          }
        ]
      }
    },
    {
      "EntityScope": "LiveAndHistory",
      "RootNode": {
        "NodeType": "Logical",
        "Operator": "AND",
        "Children": [
          {
            "NodeType": "CrossEntityCondition",
            "LiveProperty": "AdmissionDate",
            "HistoryProperty": "ThroughDate",
            "RelationalOperator": "<",
            "ValueType": "DaysOf",
            "Value": "2",
            "IncludeLiveAndHistoryClaims": "0"
          },
          {
            "NodeType": "CrossEntityCondition",
            "LiveProperty": "Charges",
            "HistoryProperty": "RevCode",
            "RelationalOperator": "<=",
            "ValueType": "GroupBy",
            "Value": "5000",
            "IncludeLiveAndHistoryClaims": "1"
          }
        ]
      }
    }
  ]
}

方案优势

  • 逻辑与实体分离:逻辑节点只处理组合逻辑,条件节点专注过滤规则,新增实体只需扩展EntityScope或新增对应条件节点类型,无需修改整体结构
  • 模型一致:单实体和跨实体的条件节点各自保持结构统一,解析时可通过NodeType区分处理
  • 扩展性强:新增逻辑运算符(如NOT)或实体类型,只需扩展枚举值,JSON结构无需大幅改动
  • 直观易维护:层级结构对应SQL的WHERE子句逻辑,符合你的SQL背景,理解和维护成本低

C#处理简化思路

不用复杂的OOP设计,可通过两个基础类(或结构体)映射节点:

public enum NodeType { Logical, Condition, CrossEntityCondition }
public enum LogicalOperator { AND, OR, NOT }

public class LogicalNode
{
    public NodeType NodeType { get; set; } = NodeType.Logical;
    public LogicalOperator Operator { get; set; }
    public List<object> Children { get; set; } = new();
}

public class ConditionNode
{
    public NodeType NodeType { get; set; } = NodeType.Condition;
    public string Property { get; set; }
    public string Operator { get; set; }
    public string RelationalOperator { get; set; }
    public string ValueType { get; set; }
    public object Value { get; set; }
}

public class CrossEntityConditionNode
{
    public NodeType NodeType { get; set; } = NodeType.CrossEntityCondition;
    public string LiveProperty { get; set; }
    public string HistoryProperty { get; set; }
    public string RelationalOperator { get; set; }
    public string ValueType { get; set; }
    public string Value { get; set; }
    public string IncludeLiveAndHistoryClaims { get; set; }
}

解析时通过NodeType判断节点类型,递归遍历逻辑节点的子节点,即可生成对应的规则决策逻辑。

内容的提问来源于stack exchange,提问作者user176047

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 16:35:07