【问题标题】:How to find value in string array using JSONPath?如何使用 JSONPath 在字符串数组中查找值?
【发布时间】:2017-01-18 20:07:55
【问题描述】:

我正在尝试过滤其中 hasContent 属性为 true 且 legalStatutes 字符串数组包含带有“AIA”的字符串的 formParagraph 对象列表。问题是“AIA”在数组中并不总是处于零位置。

仅当“AIA”是数组中的第一项时,以下 JSONPath 才有效。

   $.businessResponse.formParagraphList[ ? ((@.hasContent == true) && (@.legalStatutes[0] == 'AIA'))] {
    "rbacUser": null,
    "businessResponse": {
        "formParagraphList": [{
                "id": 1261,
                "hasContent": true,
                "legalStatutes": [
                    "Pre_AIA",
                    "AIA"
                ]
            },
            {
                "id": 67,
                "hasContent": true,
                "legalStatutes": [
                    "AIA",
                    "Pre_AIA"
                ]
            },
            {
                "id": 68,
                "hasContent": true,
                "legalStatutes": [
                    "Pre_AIA"
                ]
            }
        ]
    }
}

有没有办法使用 JSONPath 在不知道它的索引的情况下检查 JSON 对象内的字符串数组中是否存在特定字符串?

【问题讨论】:

    标签: c# json.net jsonpath


    【解决方案1】:

    好的,找到了解决我的问题的方法。使用.indexOf()方法检查字符串是否存在于数组中。

    $.businessResponse.formParagraphList[?((@.hasContent==true) && (@.legalStatutes.indexOf('AIA') > -1))]
    

    更新: 上面的 JSONPath 在在线评估器中有效,但在 Newtonsoft for C# 中不起作用。这就是我要使用这个库的内容。

    $.businessResponse.formParagraphList[?(@.hasExaminerNote==true && @.legalStatutes[*]=='Pre_AIA')]
    

    【讨论】:

    • 请注意,这在 c# 中使用 newtonsoft 不起作用。当我找到它时会发布该答案
    猜你喜欢
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2015-12-31
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 2019-08-05
    相关资源
    最近更新 更多