java - 修改具有 jsonpath 的 Json

标签 java json

我有一个具有以下示例结构的 json 文件

{
  "contract": {
    "marketScope": "AT",
    "businessPartner": "GBM",
    "salesChannelInformation": {
      "salesChannelCode": "Integrated",
      "salesChannel": "B-Partner information 1"
    }
}

给定一个jsonpath,我想修改一个特定的键值。

例如 将“contract.salesChannelInformation.salesChannelCode”更改为值“Integrated-Test”

目前我有以下代码:

public void setProperty(String fileString,String path, String value) {

    if(JsonPath.given(fileString).get(path) == null){
        Assert.fail("Path does not exist on json file");
    }else {

        try {
            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(fileString);


            System.out.println(jsonObject);

            String[] tokens = path.split("\\.");
            for (String token : tokens) {
                System.out.println(token);
                // Iterate the JsonObject, reach the key and modify the value

            }

        } catch (ParseException ex) {
            ex.printStackTrace();
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }
    }


}

我希望这样修改json文件

{
  "contract": {
    "marketScope": "AT",
    "businessPartner": "GBM",
    "salesChannelInformation": {
      "salesChannelCode": "Integrated-Test",
      "salesChannel": "B-Partner information 1"
    }
}

最佳答案

com.jayway.jsonpath.DocumentContext.set() 可用于修改 JSON 中元素的值

   /**
     * Set the value a the given path
     *
     * @param path      path to set
     * @param newValue  new value
     * @return a document context
     */
    DocumentContext set(JsonPath path, Object newValue);

图书馆:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version><!--Version--></version>
</dependency>

代码片段:

String json = "{\n" +
                "\t\"contract\": {\n" +
                "\t\t\"marketScope\": \"AT\",\n" +
                "\t\t\"businessPartner\": \"GBM\",\n" +
                "\t\t\"salesChannelInformation\": {\n" +
                "\t\t\t\"salesChannelCode\": \"Integrated\",\n" +
                "\t\t\t\"salesChannel\": \"B-Partner information 1\"\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}";
        DocumentContext parsedDataContext = jsonParseContext.parse(json);

        parsedDataContext.set("$..contract.salesChannelInformation.salesChannelCode", "Integrated-Test");

        System.out.println(parsedDataContext.jsonString());

输出:

{"contract":{"marketScope":"AT","businessPartner":"GBM","salesChannelInformation":{"salesChannelCode":"Integrated-Test","salesChannel":"B-Partner information 1"}}}

关于java - 修改具有 jsonpath 的 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55592579/

相关文章:

c# - void AjaxManager_AjaxRequest 可以将 JSON 数据发送回浏览器吗?

java - Android 从 http 请求获取 json 引发 IOException : Attempted read on closed stream

json - 如何打开和读取 JSON 文件?

java - 如何在java中使用方法来使用按位运算符?

java - List<JAXBElement< 是什么?扩展 SomeClassName>> 是什么意思?

java - 服务层和UI界面

java - 由于所需库的限制,JFrame 受限

java - 你如何在它的 Action 监听器中引用一个按钮?

java - 如何使用 Struts 2 框架构建自定义 JSON 错误响应

c# - Newtonsoft 对象序列化为字符串。预期 JObject 实例