【发布时间】:2017-05-17 07:34:14
【问题描述】:
我已将 JSON 文件转换为地图,我需要使用 JAVA 一次修改不同键的多个值。
以下是地图转换后的JSON文件:
{
"name": "",
"numberID": null,
"StartDate": "",
"EndDate": "",
"count": null,
"level": null,
"discipline": null,
"paymentModel": null,
"ownerName" : null,
"coursepackType": null,
"note": null
}
我需要从上面的 JSON 一次更新多个值,我想为以下键添加值:
- 姓名
- 开始日期
- 结束日期
- 计数
- 所有者姓名
我现在所写的只是更新单个值的方法,这已经是非常线性的: 使用以下系列函数:
String body="";
File testDataJsonfile = new File("path/xxx.json");
JsonNode testJSONNodes = getJsonNodes(testDataJsonfile);
Map<String, Object> mapWithJSONNodes = convertJSONTOMAP(testJSONNodes);
body = updateValue(mapWithJSONNodes, "name", "abc")
public String updateValue(Map<String,Object> map ,String key,String value) throws JsonProcessingException{
map.put(key, value);
String convertedJSONFile = new ObjectMapper().writeValueAsString(map);
return convertedJSONFile;
}
有什么方法可以动态地实现我想要的,可能传递一个数组列表左右? 如果需要任何其他规格,请告诉我。
【问题讨论】:
-
Map 有一个 putAll 方法...