我需要使用不确定路径从 json 路径获取原始数据类型

I need to get the primitive data type from json path using in-definite path

鉴于此文档:

{
    "slide": [{
        "title": {
            "fontName": "Open Sans",
            "top": 188,
            "left": -22,
            "width": 597,
            "fontSize": 45,
            "valign": "bottom",
            "presetType": "Parallelogram",
            "fill": "#6bcad5",
            "halign": "left",
            "fontColor": "#f4dde6",
            "height": 192
        }
    }, {
        "picture": {
            "top": 25,
            "left": 54,
            "width": 1,
            "colorMode": "GRAYSCALE",
            "presetType": "Snip_Same_Side_Rect",
            "height": 1
        }
    }]
}

以下代码returns[54]:

JSONArray obj = ctx.read("$.slide[?(@.picture)].picture.left");

但我需要一个原始类型,仍然保持无限路径。

根据the docs

Please note, that the return value of jsonPath is an array, which is also a valid JSON structure. So you might want to apply jsonPath to the resulting structure again or use one of your favorite array methods as sort with it.

注意:如果您正在使用 Java 实现,那么已经为此提出了 issue 并且对该问题的回应重申了上述观点。

因此,只要您使用过滤器,就需要两次调用,例如:

String json = "...";

DocumentContext ctx = JsonPath.parse(json);

// capture the JSONArray
JSONArray obj = ctx.read("$.slide[?(@.picture)].picture.left");

// read the first value from the JSONArray
// prints "54"
System.out.println(obj.get(0)); 

// alternatively, push the JSON representation of the JSONArray back through JsonPath
Integer value = JsonPath.read(obj.toJSONString(), "$[0]");
// prints 54
System.out.println(value);