【问题标题】:JSONPatch escape slash '/' from JSONPatch+JSONJSONPatch 转义斜线“/”来自 JSONPatch+JSON
【发布时间】:2021-07-15 15:19:25
【问题描述】:

我低于 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
        }
}

使用带有以下 JSONPatch 有效负载 API 的邮递员 PATCH 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"}]

该文档提到http://jsonpatch.com/#json-pointer 我们必须使用~0~1 转义字符但还没有运气,我使用~1 转义/"~1config~1version" 但它的计算结果为"/~01config~01version"

【问题讨论】:

  • 1) 这是哪种编程语言? 2)根据1,你从哪里得到JsonPointer
  • 我正在尝试使用 Java

标签: java json rest json-patch http-patch


【解决方案1】:

我认为问题出在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,它提供了有关如何在战术上构建JsonPointers 以​​及转义保留字符的含义的指导。

【讨论】:

  • 欢迎您@SwapnilKotwal。我很高兴听到这个答案很有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
相关资源
最近更新 更多