如何进入 Java JSONpath 的所有输出路径,以便通过它们自己的函数替换现有值
How to get in Java all output paths of a JSONpath in order to replace existing values by a function of themseleves
假设我在 上有这个 .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
}
我这样做的想法是指定 json路径
$.store.book[*].price
并进入return输出路径列表(如本网站:https://jsonpath.com/)
[ "$.store.book[0].price", "$.store.book[1].price", "$.store.book[2].price", "$.store.book[3].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);
}
}
以上代码打印:
{
"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
- Java BigDecimal: Round to the nearest whole value
假设我在 上有这个 .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
}
我这样做的想法是指定 json路径
$.store.book[*].price
并进入return输出路径列表(如本网站:https://jsonpath.com/)
[ "$.store.book[0].price", "$.store.book[1].price", "$.store.book[2].price", "$.store.book[3].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);
}
}
以上代码打印:
{
"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
- Java BigDecimal: Round to the nearest whole value