调用UpdateJSONFileData方法时JSON文件数据被覆盖,无法追加问题
问题:JSON文件更新时数据被覆盖,无法追加内容
调用writeTojsonFile方法可正常向JSON文件写入数据,但调用UpdateJSONFileData方法时,原有数据被覆盖而非追加。需求是支持两种写入方式:直接传入键值对,或传入对象,实现数据追加合并。
当前实现代码
public static void writeTojsonFile(String key, String value, String fileName, Object object) { JSONObject jsonObject = new JSONObject(); ObjectMapper objectMapper= new ObjectMapper(); try{ FileWriter file = new FileWriter(filePath); if(object == null) { jsonObject.put(key, value); file.write(jsonObject.toJSONString()); file.flush(); } else { objectMapper.writeValue(new File(fileName), object); } }catch (IOException e) { e.printStackTrace(); } } public static void UpdateJSONFileData(String Key, String value, String filePath, Object object) throws FileNotFoundException, IOException, ParseException { ObjectMapper objectMapper = new ObjectMapper(); ObjectNode objectNode = new ObjectNode(null); objectNode = objectMapper.readValue(new File(filePath), objectNode.getClass()); if(object == null) { objectNode.put(key, value); objectMapper.writeValue(new File(filePath), objectNode); } else{ objectMapper.writeValue(new File(filePath), object); } }
调用示例
writeTojsonFile("Role", "Engineer", "/src/employeeInfo.json", null); JSONObject empInfo = new JSONObject(); empInfo.put("ID","1234"); empInfo.put("Name","John"); UpdateJSONFileData("","", "/src/employeeInfo.json", empInfo);
期望与实际输出
- 期望输出:
{"Role":"Engineer","ID":"1234", "Name":"John"} - 实际输出(被覆盖后):
{"ID":"1234", "Name":"John"}
解决方案
问题核心是UpdateJSONFileData方法中,传入对象时直接覆盖了文件,未合并原有数据;同时存在变量名不一致、ObjectNode初始化错误等问题。修正后的代码如下:
修正后的UpdateJSONFileData方法
public static void UpdateJSONFileData(String key, String value, String filePath, Object object) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); File jsonFile = new File(filePath); // 初始化ObjectNode:文件不存在则创建空节点,否则读取原有内容 ObjectNode objectNode; if (!jsonFile.exists()) { objectNode = objectMapper.createObjectNode(); } else { objectNode = objectMapper.readValue(jsonFile, ObjectNode.class); } if (object == null) { // 传入键值对时,追加到节点 if (key != null && !key.isEmpty() && value != null) { objectNode.put(key, value); } } else { // 传入对象时,转为节点并合并到原有内容中 ObjectNode newObjectNode = objectMapper.valueToTree(object); objectNode.setAll(newObjectNode); } // 将合并后的节点写入文件 objectMapper.writeValue(jsonFile, objectNode); }
补充优化writeTojsonFile方法(可选,避免初始写入覆盖已有数据)
如果希望初始写入也支持合并而非覆盖,可以做类似修改:
public static void writeTojsonFile(String key, String value, String fileName, Object object) { ObjectMapper objectMapper = new ObjectMapper(); File jsonFile = new File(fileName); ObjectNode objectNode; try { if (!jsonFile.exists()) { objectNode = objectMapper.createObjectNode(); } else { objectNode = objectMapper.readValue(jsonFile, ObjectNode.class); } if (object == null) { if (key != null && !key.isEmpty() && value != null) { objectNode.put(key, value); } } else { ObjectNode newObjectNode = objectMapper.valueToTree(object); objectNode.setAll(newObjectNode); } objectMapper.writeValue(jsonFile, objectNode); } catch (IOException e) { e.printStackTrace(); } }
关键修复点
- 统一变量名:将参数
Key改为key,避免编译错误。 - 正确初始化ObjectNode:使用
objectMapper.createObjectNode()而非传入null。 - 合并对象数据:传入对象时,转为ObjectNode后通过
setAll方法合并到原有节点,而非直接覆盖文件。 - 增加文件存在性判断:处理文件不存在的场景,避免读取失败。
修改后调用示例代码,即可得到期望的合并后JSON内容。
内容的提问来源于stack exchange,提问作者vss
相关产品推荐
相关产品推荐

