简体   繁体   中英

Replace json value by jsonpath

we have such a json string:

{
"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:

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. The above json structure is just one example, the json path for "key6" is quite different in different json. We couldn't write the exact jsonpath for this kind of replacement. How can we just replace the value of "key6" which has no child by java? Thanks a lot.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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