我已经在下面的JSON中更新了几个字段
{
"process": "my-process",
"pod": "some-pod",
"org": "some-org",
"config": {
"version": "436_601_83.0.0",
"path": "companyName/ccg",
"description": "update the version",
"dependencies": null
}
}将postman补丁API调用与以下JSONPatch有效负载API配合使用可以正常工作。
[
{
"path": "/config/version",
"op": "replace",
"value": "436_605_83.0.0"
},
{
"path": "/config/description",
"op": "replace",
"value": "foo bar"
}
]但是,我想用Java实现同样的功能。我试过了
JsonPatch jsonPatch = new JsonPatch(
Arrays.asList(
new ReplaceOperation(JsonPointer.of("/config/version"),
new TextNode("436_605_83.0.0"))
)
);它的计算结果为:
[{"op":"replace","path":"/~1config~1version","value":"436_605_83.0.0"}]这篇文档提到,我们必须使用~0和~1对字符进行转义,但还没有成功,我使用~1转义了/,即"~1config~1version",但它的计算结果为"/~01config~01version"
发布于 2021-07-19 00:07:12
我认为问题出在JsonPointer的定义中。请尝试一下这样的东西:
JsonPatch jsonPatch = new JsonPatch(
Arrays.asList(
new ReplaceOperation(
// Note we should provide the different paths tokens here
JsonPointer.of("config", "version"),
new TextNode("436_605_83.0.0")
)
)
);或者,等效地:
JsonPatch jsonPatch = new JsonPatch(
Arrays.asList(
new ReplaceOperation(
// Create the JsonPointer with the full path
new JsonPointer("/config/version"),
new TextNode("436_605_83.0.0")
)
)
);请参阅this test,它提供了有关如何从战术上构建JsonPointer的指导,以及转义保留字符的含义。
https://stackoverflow.com/questions/68396523
复制相似问题