将数据导出到json文件或将json文件导入到数据库 - Go语言中文社区

将数据导出到json文件或将json文件导入到数据库


直接参考我的代码吧,可以不要在意业务逻辑,直接看JSON处理相关的部分
jar包:
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;
}
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_43222869/article/details/106859335
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢