如何重写完整更新的 JSON,而不仅仅是嵌套部分。

huangapple go评论70阅读模式
英文:

How to rewrite full updated json instead of only the nested part

问题

我有一个名为test.json的json文件,内容如下:

{
  "Added": {
    "type": "K",
    "newmem": {
      "IDNew": {
        "id": "777709",
        "type": "LOP"
      },
      "birthDate": "2000-12-09"
    },
    "code": "",
    "newest": {
      "curlNew": "",
      "addedForNew": ""
    }
  }
}

我能够更新json中的id并写回文件,使用以下代码:

FileReader reader = new FileReader(filePath); //文件路径为D://.../test.json
String keyToUpdate = "Added.newmem.IDNew.id";
String[] keyArr = jsonKey.split("\\.");
String keyToUpdate = keyArr[keyArr.length-1];
keyArr.removeElement(keyArr, keyToUpdate);
for (String key: keyArr){ 
   jsonObject = (JSONObject) jsonObject.get(key);
}

jsonObject.put("id",12345);
FileOutputStream outputStream = new FileOutputStream(filePath);
byte[] strToBytes = jsonObject.toString().getBytes();
outputStream.write(strToBytes);

然而,与其用更新后的值仅重写嵌套部分,不如重写整个json。我该怎么做?

英文:

I have a json file called test.json that looks like:

{
  "Added": {
    "type": "K",
    "newmem": {
      "IDNew": {
        "id": "777709",
        "type": "LOP"
      },
      "birthDate": "2000-12-09"
    },
    "code": "",
    "newest": {
      "curlNew": "",
      "addedForNew": ""
    }
  }
}

I am able to update the id in the json and write back to the file using the following code:

FileReader reader = new FileReader(filePath); //where file path = D://.../test.json
String keyToUpdate = "Added.newmem.IDNew.id"
String[] keyArr = jsonKey.split("\\." );
String keyToUpdate = keyArr [keyArr.length-1]
keyArr.removeElement(keyArr, keyToUpdate)
for (String key: keyArr){ 
   jsonObject = (JSONObject) jsonObject.get(key);
}

jsonObject.put("id",12345);
FileOutputStream outputStream = new FileOutputStream(filePath);
byte[] strToBytes = jsonObject.toString().getBytes();
outputStream.write(strToBytes);

However, instead of re-writing the entire json with the updated value, it's only writing:

  {
    "id": "777709",
    "type": "LOP"
  }

What can I do to re-write the entire json instead of only the nested part of it?

答案1

得分: 0

你的jsonObject在更新前只包含“IDNew”节点。

最好的方法是使用xPath来自Jayway

    private static final Configuration configuration = Configuration.builder()
        .jsonProvider(new JacksonJsonNodeJsonProvider())
        .mappingProvider(new JacksonMappingProvider())
        .build();
    String originalJson = "{\"Added\":{\"type\":\"K\",\"newmem\":{\"IDNew\":{\"id\":\"777709\",\"type\":\"LOP\"},\"birthDate\":\"2000-12-09\"},\"code\":\"\",\"newest\":{\"curlNew\":\"\",\"addedForNew\":\"\"}}}";
    JsonNode updatedJson = JsonPath.using(configuration).parse(originalJson).set("$.Added.IDNew.id", 12345).json();

    System.out.println(updatedJson.toString());
    }
英文:

Your jsonObject before update only contains the "IDNew" node.

The best way to do this is using xPath from Jayway

private static final Configuration configuration = Configuration.builder()
    .jsonProvider(new JacksonJsonNodeJsonProvider())
    .mappingProvider(new JacksonMappingProvider())
    .build();



    String originalJson = "{\r\n"
    		+ "  \"Added\": {\r\n"
    		+ "    \"type\": \"K\",\r\n"
    		+ "    \"newmem\": {\r\n"
    		+ "      \"IDNew\": {\r\n"
    		+ "        \"id\": \"777709\",\r\n"
    		+ "        \"type\": \"LOP\"\r\n"
    		+ "      },\r\n"
    		+ "      \"birthDate\": \"2000-12-09\"\r\n"
    		+ "    },\r\n"
    		+ "    \"code\": \"\",\r\n"
    		+ "    \"newest\": {\r\n"
    		+ "      \"curlNew\": \"\",\r\n"
    		+ "      \"addedForNew\": \"\"\r\n"
    		+ "    }\r\n"
    		+ "  }\r\n"
    		+ "}";

JsonNode updatedJson = JsonPath.using(configuration).parse(originalJson).set("$.Added.IDNew.id", 12345).json();

System.out.println(updatedJson.toString());
}

huangapple
  • 本文由 发表于 2023年2月8日 23:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定