如何在Java中获取JSONpath的所有输出路径,以便用Seleves函数替换现有值

如何在Java中获取JSONpath的所有输出路径,以便用Seleves函数替换现有值,java,json,jsonpath,Java,Json,Jsonpath,假设我有一个.json,我想应用一个函数来计算书的价格: { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "cate

假设我有一个.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
并返回输出路径列表(如本网站所示:)

或者最好是路径和值之间的直接映射

[ {"$.store.book[0].price":8.95}, {"$.store.book[1].price":12.99}, {"$.store.book[2].price":8.99}, {"$.store.book[3].price":22.99} ]
然后在列表上循环,对每个值应用函数,并使用新值设置json

但我找不到如何获得路径列表,我该怎么做?(或者,如果您有一些东西可以直接用它们自身的函数替换该值,我也会这么认为:))


PS:我给出的.json只是一个例子,我需要它来嵌套.json,以列出您需要用配置对象解析文档的路径。之后,您需要再次解析它以进行更新:

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);
    }
}
另见:


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

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
}