-
需求:编写工具类要尽可能不用任意一个 jar 包依赖,或者尽可能精简
-
原有数据:
ArpsParams{actualProduction=[{mouth=2022-07, oilProdDaily=366088.33}, {mouth=2022-08, oilProdDaily=380806.78}], predictionSegment=[{mouth=2022-07, oilProdDaily=367832.31}, {mouth=2022-08, oilProdDaily=368310.88}], predictionProduction=[{mouth=2023-10, oilProdDaily=374462.45}, {mouth=2023-11, oilProdDaily=374949.65}, {mouth=2023-12, oilProdDaily=375437.49}, {mouth=2024-01, oilProdDaily=375925.96}, {mouth=2024-02, oilProdDaily=376415.07}], methodName='指数递减', formula='Q=367354.35*e^(--0.001*t)'}
-
现有要求转换成 JSON 形式显示:
{ "actualProduction": [ { "mouth": "2022-07", "oilProdDaily": 366088.33 }, { "mouth": "2022-08", "oilProdDaily": 380806.78 } ], "predictionSegment": [ { "mouth": "2022-07", "oilProdDaily": 367832.31 }, { "mouth": "2022-08", "oilProdDaily": 368310.88 } ], "predictionProduction": [ { "mouth": "2023-10", "oilProdDaily": 374462.45 }, { "mouth": "2023-11", "oilProdDaily": 374949.65 }, { "mouth": "2023-12", "oilProdDaily": 375437.49 }, { "mouth": "2024-01", "oilProdDaily": 375925.96 }, { "mouth": "2024-02", "oilProdDaily": 376415.07 } ], "methodName": "指数递减", "formula": "Q=367354.35*e^(--0.001*t)" }
-
方法一:引入第三方依赖,如
jackson-databind
,然后使用:import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper objectMapper = new ObjectMapper(); String arpsParamsJson = objectMapper.writeValueAsString(arpsParams); System.out.println(arpsParamsJson); // JSON样式
-
方法二:拼接,推荐使用
StringBuilder builder = new StringBuilder();
// 开始拼接: StringBuilder builder = new StringBuilder("{"); // 实际数据 actualProduction: List<Map<String, Object>> actual = arpsParams.getActualProduction(); builder.append("\"actualProduction\": ["); for (int i = 0; i < actual.size(); i++){ // 获取 mouth 和 oilProdDaily 的值 String mouth = (String) actual.get(i).get("mouth"); Double oilProdDaily = (Double) actual.get(i).get("oilProdDaily"); builder.append("{\"mouth\": \"" + mouth + "\","); builder.append("\"oilProdDaily\": " + oilProdDaily + "},"); } int lastCommaIndex1 = builder.lastIndexOf(","); if (lastCommaIndex1 != -1) { // 删除最后一个逗号 builder.deleteCharAt(lastCommaIndex1); } builder.append("],"); // 拟合数据 predictionSegment: List<Map<String, Object>> segment = arpsParams.getPredictionSegment(); builder.append("\"predictionSegment\": ["); for (int i = 0; i < segment.size(); i++){ // 获取 mouth 和 oilProdDaily 的值 String mouth = (String) segment.get(i).get("mouth"); Double oilProdDaily = (Double) segment.get(i).get("oilProdDaily"); builder.append("{\"mouth\": \"" + mouth + "\","); builder.append("\"oilProdDaily\": " + oilProdDaily + "},"); } int lastCommaIndex2 = builder.lastIndexOf(","); if (lastCommaIndex2 != -1) { builder.deleteCharAt(lastCommaIndex2); } builder.append("],"); // 预测数据 predictionProduction: List<Map<String, Object>> production = arpsParams.getPredictionProduction(); builder.append("\"predictionProduction\": ["); for (int i = 0; i < production.size(); i++){ // 获取 mouth 和 oilProdDaily 的值 String mouth = (String) production.get(i).get("mouth"); Double oilProdDaily = (Double) production.get(i).get("oilProdDaily"); builder.append("{\"mouth\": \"" + mouth + "\","); builder.append("\"oilProdDaily\": " + oilProdDaily + "},"); } int lastCommaIndex3 = builder.lastIndexOf(","); if (lastCommaIndex3 != -1) { builder.deleteCharAt(lastCommaIndex3); } builder.append("],"); // 模型类型和表达式: String methodName = arpsParams.getMethodName(); String formula = arpsParams.getFormula(); builder.append("\"methodName\": \"" + methodName + "\",\"formula\": \"" + formula + "\"}"); // 以JSON形式输出显示 System.out.println(builder);