将一个map集合转换成json文件并导出:
// 数据准备:
JSONObject resData = new JSONObject();
ArrayList<Map> list = new ArrayList<>();
JSONObject root = new JSONObject();
root.put("root1","root1");
list.add(root);
list.add(root);
list.add(root);
resData.put("root",list);
System.out.println(resData.toStringPretty());
// 格式化json数据
String jsonString = resData.toStringPretty();
// 输出
File file = new File("D:/demo1.json");
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
// 将格式化后的字符串写入文件
Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
write.write(jsonString);
write.flush();
write.close();
}catch (Exception e){
e.printStackTrace();
}
两种方法实现固定排序:
1、JSONObject jsonObj = new JSONObject(new LinkedHashMap<String, Object>());
2、JSONObject jsonObj = new JSONObject(true);
本文介绍了如何使用Java将ArrayList<Map>转换为格式化的JSON文件,并展示了两种实现固定排序的方法,涉及JSONObject和LinkedHashMap的运用。
4086

被折叠的 条评论
为什么被折叠?



