【问题标题】:Can't get the desired properties via JsonPath evaluate method无法通过 JsonPath 评估方法获取所需的属性
【发布时间】:2023-02-17 07:40:48
【问题描述】:

我有一个 json 架构,用于标记需要处理的特殊属性,我想通过 JsonPath.Evaluate 查询这些属性。

这是说明问题的架构的一部分

{
    "type": "object",
    "properties": {
        "period": {
            "description": "The period in which the rule applies",
            "type": "object",
            "properties": {
                "start": {
                    "type": "string",
                    "format": "date-time"
                },
                "end": {
                    "type": "string",
                    "format": "date-time"
                }
            },
            "required": [
                "start"
            ],
            "x-updateIndicatorProperties": [
                "start"
            ]
        },
        "productType": {
            "type": "string"
        },
        "x-updateIndicatorProperties": [
            "productType"
        ]
    }
}

我想获取“x-updateIndicatorProperties”属性的 JsonPath,以便我可以查询要处理的实际属性。 对于这个例子,预期的结果是

[
  "$['properties']['x-updateIndicatorProperties']",
  "$['properties']['period']['x-updateIndicatorProperties']"
]

我一直在尝试获取一个 JsonPath 表达式来查询这些属性。 目前我只是迭代所有属性并手动过滤它们: “$..*”

我也试过使用: $..['x-updateIndicatorProperties']

这行得通。但它返回了很多重复项。对于上面的例子,我得到了 5 个结果而不是预期的 2 个。可以在这里演示:https://json-everything.net/json-path

假设我不能影响架构本身,只能影响遍历它的代码, 任何人都可以帮助表达以获得预期的结果或任何其他方式来实现相同的结果吗?

堆栈是 JsonPath 0.2.0、.net 6 和 system.text.json。

【问题讨论】:

  • 我希望递归体面的路径(你发布的$..[])应该有效。不知道为什么它返回重复项。这似乎是一个错误。你能在我的 GitHub 回购上打开一个问题吗?

标签: jsonpath c#-6.0 system.text.json json-everything


【解决方案1】:

在将使用递归下降 (..) 的路径解析为带引号的属性名称选择器 (['foo']) 时,这是库中的一个错误。所以对于 $..['foo'] 形式的任何路径都会发生这种情况。

我已经解决了这个问题并发布了 0.2.1 版。

【讨论】:

  • 那很快!非常感谢!
  • 刚刚更新了实现。奇迹般有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
相关资源
最近更新 更多