社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
直接参考我的代码吧,可以不要在意业务逻辑,直接看JSON处理相关的部分
jar包:
public String exportCalibrateSourceRecord(String taskId, String exportPath) {
//校验参数
if (StringUtil.isEmpty(taskId) || StringUtil.isEmpty(exportPath)) {
return null;
}
//创建文件夹
File file = new File(exportPath);
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
//查询原始记录基本信息和原始记录
String sqlForBaseInfo = "select * from xa401_calibrate_base_241 x where x.xa401_project_241_id = ?";
List<Map<String, Object>> baseInfoList = metaDaoFactory.getJdbcTemplate().queryForList(sqlForBaseInfo, taskId);
//循环查询各个基本信息下的原始记录并添加到对应基本信息的Map中
for (int i = 0; i < baseInfoList.size(); i++) {
//查询基本信息Id下的原始数据
String baseInfoId = (String) baseInfoList.get(i).get("ID");//基本信息ID
String sqlForRawInfo = "select * from xa401_calibrate_raw_241 c where c.xa401_calibrate_base_241_id = ?";
List<Map<String, Object>> rawInfoList = metaDaoFactory.getJdbcTemplate().queryForList(sqlForRawInfo, baseInfoId);
//将原始数据添加到基本信息的Map中
baseInfoList.get(i).put("rawInfoList", rawInfoList);
}
//将原始记录基本信息(含原始记录)转换成JSON对象并写入文件
Map<String, Object> baseInfoMap = new HashMap<>();
baseInfoMap.put("baseInfoList", baseInfoList);
JSONObject baseInfoJsonObject = new JSONObject(baseInfoMap);
//导出JSON文件
File jsonFile;
try {
jsonFile = new File(exportPath + "SourceRecordCalibrateBaseInfo_" + taskId + ".json");
if (!jsonFile.exists()) {
jsonFile.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(jsonFile);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(baseInfoJsonObject.toJSONString());
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return jsonFile.getAbsolutePath();
}
public boolean importCalibrateSourceRecord(String taskId, String sourcePath) {
//校验参数
if (StringUtil.isEmpty(taskId) || StringUtil.isEmpty(sourcePath)) {
return false;
}
File sourceFile = new File(sourcePath);
if (!sourceFile.exists() || !sourcePath.endsWith(".json")) {
return false;
}
//读取文件
String jsonString = readJsonFile(sourceFile);
JSONObject jsonObject = JSON.parseObject(jsonString);
//获取基本信息和原始记录并保存到数据库
JSONArray baseInfoList = jsonObject.getJSONArray("baseInfoList");//基本信息列表
for (int i = 0; i < baseInfoList.size(); i++) {
//获取单个基本信息及其下的原始记录列表
JSONObject baseInfo = (JSONObject) baseInfoList.get(i);//基本信息
JSONArray rawInfoList = baseInfo.getJSONArray("rawInfoList");//该基本信息下的原始记录列表
baseInfo.remove("rawInfoList");
//更改基本信息的ID和所属的试验任务
String baseInfoId = System.currentTimeMillis() + i + "";
baseInfo.put("ID", baseInfoId);
baseInfo.put("XA401_PROJECT_241_ID", taskId);
//将基本信息存入数据库
IBusinessModel baseBm = baseUtil.getBusinessModelByName("XA401_CALIBRATE_BASE");
baseUtil.insertBmData(baseBm, baseInfo);
//原始记录
for (int j = 0; j < rawInfoList.size(); j++) {
//获取基本信息并更改ID和所属的基本信息
JSONObject rawInfo = (JSONObject) rawInfoList.get(j);
rawInfo.put("ID", System.currentTimeMillis() + i + "" + j);
rawInfo.put("XA401_CALIBRATE_BASE_241_ID", baseInfoId);
//将原始记录存入数据库
IBusinessModel rawBm = baseUtil.getBusinessModelByName("XA401_CALIBRATE_RAW");
baseUtil.insertBmData(rawBm, rawInfo);
}
}
return true;
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!