如何在Java中获取JSON路径的所有输出路径,以便通过它们的函数替换现有值

问题描述 投票:1回答:1

假设我有这个.json巫婆我想要应用一个函数来收集这本书的价格:

{
"store": {
    "book": [
        {
            "category": "reference",
            "author": "Nigel Rees",
            "title": "Sayings of the Century",
            "price": 8.95
        },
        {
            "category": "fiction",
            "author": "Evelyn Waugh",
            "title": "Sword of Honour",
            "price": 12.99
        },
        {
            "category": "fiction",
            "author": "Herman Melville",
            "title": "Moby Dick",
            "isbn": "0-553-21311-3",
            "price": 8.99
        },
        {
            "category": "fiction",
            "author": "J. R. R. Tolkien",
            "title": "The Lord of the Rings",
            "isbn": "0-395-19395-8",
            "price": 22.99
        }
    ],
    "bicycle": {
        "color": "red",
        "price": 19.95
    }
},
"expensive": 10
}

我这样做的想法是指定jsonpath

$.store.book[*].price

并获得输出路径列表(如本网站:qazxsw poi)

https://jsonpath.com/

甚至更好的是路径和价值之间的直接映射

[ "$.store.book[0].price", "$.store.book[1].price", "$.store.book[2].price", "$.store.book[3].price" ]

然后在列表上循环以在每个值上应用函数并使用新值设置json

但我找不到如何获得该路径列表,我该怎么做? (或者,如果你有什么东西可以通过自己的函数直接替换值,我也会接受:))

PS:我给的.json只是一个例子,我需要它.json方式比它更嵌套

java json jsonpath
1个回答
0
投票

要列出需要使用配置对象解析文档的路径。之后,您需要再次解析它以进行更新:

[ {"$.store.book[0].price":8.95}, {"$.store.book[1].price":12.99}, {"$.store.book[2].price":8.99}, {"$.store.book[3].price":22.99} ]

上面的代码打印:

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.internal.JsonContext;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

public class JsonPathApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        // read paths
        List<String> paths = JsonPath
                .using(Configuration.builder().options(Option.AS_PATH_LIST).build())
                .parse(jsonFile)
                .read("$.store.book[*].price", List.class);

        // compile paths
        List<JsonPath> jsonPaths = paths
                .stream()
                .map(p -> JsonPath.compile(p))
                .collect(Collectors.toList());

        // parse once for reading/updating
        JsonContext document = (JsonContext) JsonPath.parse(jsonFile);
        jsonPaths.forEach(path -> {
            BigDecimal value = document.read(path, BigDecimal.class);
            document.set(path, transform(value));
        });

        System.out.println(document.jsonString());
    }

    private static BigDecimal transform(BigDecimal value) {
        return value.setScale(0, RoundingMode.HALF_UP);
    }
}

也可以看看:

  • { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 9 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 13 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 9 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 23 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 }
  • JSONPath
© www.soinside.com 2019 - 2024. All rights reserved.