JSON-在所有数组元素内设置字段值

时间:2018-12-21 03:49:02

标签: java jsonpath

我有一个类似下面的JSON。

  

store.book [*]。author [*]。share

我需要用(当前值+10)更新份额值。 如果份额值为9.2,则更新后的值应为(19.2)。新值也可以是复杂的逻辑。因此,基本上可以获取当前值并根据逻辑更新新值。

{
    "store": {
        "book": [
            {
                "category": "reference",
                "title": "Sayings of the Century",
                "price": 8.95,
                "author": [
                 {
                   "name": "Mike Waugh",
                   "share": "9.2"
                 },
                 {
                   "name": "Shane Warne",
                   "share": "8.2"
                 }
                ]
            },
            {
                "category": "fiction",
                "title": "Sword of Honour",
                "price": 9.0,
                "author": [
                 {
                   "name": "Steve Bro",
                   "share": "9.6"
                 },
                 {
                   "name": "Mikky waugh",
                   "share": "9.7"
                 }
               ]
            },
            {
                "category": "fiction",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99,
                 "author": [
                 {
                   "name": "Evelyn Dough",
                   "share": "6.6"
                 },
                 {
                   "name": "Mark waugh",
                   "share": "5.7"
                 }
               ]
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

我有一个要更新的元素。

DocumentContext json = JsonPath.using(configuration).parse(jsonInput);
String jayPath = "$..book[0].author[0].share";
String tagValue = "10"+json.get(jayPath);
System.out.println(json.set(jayPath, tagValue).jsonString());

任何基于逻辑用新值更新所有份额的方法。

也与for循环相混淆。

$..book[*]  ---> get length

for each,
$..book[0].author[*]  ------> length

for each
$..book[0].author[0].share
$..book[0].author[1].share

还有其他更好的方法吗?

0 个答案:

没有答案