英文:
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”节点。
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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论