问题
Let's say I have this .json on witch I want to apply a function that round up the price of the book:
{
"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
}
The idea I had for doing that was to specify the jsonpath
$.store.book[*].price
and get in return the list of ouput path (like on this website: https://jsonpath.com/ )
[ "$.store.book[0].price", "$.store.book[1].price", "$.store.book[2].price", "$.store.book[3].price" ]
or even better a direct mapping between the path and the value
[ {"$.store.book[0].price":8.95}, {"$.store.book[1].price":12.99}, {"$.store.book[2].price":8.99}, {"$.store.book[3].price":22.99} ]
and then making a loop over the list to apply the function on each value and setting the json with the new value
But i can't find how to get that list of path, how can i do that ? (or if you have something to directly replace the value by a function of themselves, i would take it too :) )
PS: the .json I give is just an example, I need it for .json way more nested than that
回答1:
To list path you need to parse document with configuration object. After that you need to parse it once more time for updating:
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);
}
}
Above code prints:
{
"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
}
See also:
- JSONPath
- Java BigDecimal: Round to the nearest whole value
来源:https://stackoverflow.com/questions/55423089/how-to-get-in-java-all-output-paths-of-a-jsonpath-in-order-to-replace-existing-v