简体   繁体   English

用 jsonpath 替换 json 值

[英]Replace json value by jsonpath

we have such a json string:我们有这样一个json字符串:

{
"key1": {
    "key2": {
        "key6": true
    },
    "key6": {
        "key3": [
            {
                "key4": "1",
                "key5": "1"
            },
            {
                "key4": "1",
                "key5": "1"
            }
        ]
    }

},
"from": 0
}

we want to use below code to replace latter key6's value:我们想使用下面的代码来替换后面的 key6 的值:

DocumentContext dContext = JsonPath.using(configuration).parse(objectString);
dContext.set("$.key6"+,"false"));

But as key6 has been shown in the jsonstring twice, we got the wrong value replaced.但是由于 key6 已在 jsonstring 中显示两次,因此我们替换了错误的值。 The above json structure is just one example, the json path for "key6" is quite different in different json.上面的json结构只是一个例子,“key6”的json路径在不同的json中有很大的不同。 We couldn't write the exact jsonpath for this kind of replacement.我们无法为这种替换编写确切的 jsonpath。 How can we just replace the value of "key6" which has no child by java?我们怎样才能用java替换没有孩子的“key6”的值? Thanks a lot.非常感谢。

您唯一的解决方案是使用精确的密钥,在您的情况下,如下所示:

dContext.set("$.key1.key2.key6"+,"false"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM