package com.changnan.fastjson; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONPath; import com.alibaba.fastjson2.JSONReader; @SpringBootApplication public class FastjsonApplication { public static String loggerName = "fastJson_test"; public static void main(String[] args) throws IOException { SpringApplication.run(FastjsonApplication.class, args); File file = new File("c:/workspace/java_demo/fastjson/testjsonData.json"); FileReader fileReader = new FileReader(file); Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader. read()) != -1) { sb. append((char) ch); } fileReader. close(); reader. close(); String jsonStr = sb.toString(); // https://alibaba.github.io/fastjson2/ // https://developer.aliyun.com/article/942130 long start = System. currentTimeMillis(); Logger.getLogger(loggerName).info(new Date().toString()); long end = System. currentTimeMillis(); long diff = end - start; // parseFormcustomElementJSONPath(jsonStr); // parseJSONtoMap(jsonStr); // parseJSONByClass(jsonStr); // parseFormcustomElementByMap(jsonStr); mergeEntityTest(jsonStr); Logger.getLogger(loggerName).info("Difference is : " + diff); } public static void mergeEntityTest(String jsonDataStr) { // entity root(top level) String rootPath = "$.d.results[*]"; List<Map> rootListMap = getListMapByJSONPath(rootPath, jsonDataStr); // entity level2(1:1)root String competenceSectionsPath = "$.d.results[*]"; competenceSectionsPath + = ".pmReviewContentDetail"; competenceSectionsPath + = ".competencySections.d.results[*]"; List<Map> competenceSectionsListMap = getListMapByJSONPath(competencySectionsPath, jsonDataStr); // entity level3(M:1)level2 String competenciesPath = "$.d.results[*]"; competenciesPath + = ".pmReviewContentDetail"; competenciesPath + = ".competencySections.d.results[*]"; competenciesPath + = ".competencies.d.results[*]"; List<Map> competenciesListMap = getListMapByJSONPath(competenciesPath, jsonDataStr); System.out.println("size of rootListMap:" + rootListMap.size()); System.out.println("size of competenceSectionsListMap:" + competenceSectionsListMap.size()); System.out.println("size of competenciesListMap:" + competenciesListMap.size()); // Requirement: convert level3 field row to column List<Map> mergeListMap = new ArrayList<Map>(); int p = 0; for (int i = 0; i < rootListMap. size(); i ++ ) { Map newData = new HashMap(); String formDataId = rootListMap.get(i).get("formDataId").toString(); String sectionName = competenceSectionsListMap.get(i).get("sectionName").toString(); String weightOfItem1 = ""; String weightOfItem2 = ""; String weightOfItem3 = ""; for (; p < competenciesListMap. size(); p ++ ) { String subFormDataId = competenciesListMap.get(p).get("formDataId").toString(); if (formDataId. equals(subFormDataId)) { String subItemId = competenciesListMap.get(p).get("itemId").toString(); if (subItemId. equals("1")) { weightOfItem1 = competenciesListMap.get(p).get("weight").toString(); } if (subItemId. equals("2")) { weightOfItem2 = competenciesListMap.get(p).get("weight").toString(); } if (subItemId. equals("3")) { weightOfItem3 = competenciesListMap.get(p).get("weight").toString(); } } else { break; } } newData.put("formDataId", formDataId); newData.put("sectionName", formDataId); newData.put("weightOfItem1", weightOfItem1); newData.put("weightOfItem2", weightOfItem2); newData.put("weightOfItem3", weightOfItem3); mergeListMap.add(newData); } outputListMap(mergeListMap); } public static void outputListMap(List<Map> listMaps) { listMaps.forEach(dataMap -> { dataMap.forEach((k, v) -> { Logger.getLogger(loggerName).info(k + " -->" + v); }); }); } public static List<Map> getListMapByJSONPath(String path, String jsonDataStr) { JSONPath jsonPath = JSONPath.of(path); JSONReader parser = JSONReader.of(jsonDataStr); Object result = jsonPath. extract(parser); // Logger.getLogger(loggerName).info(result.toString()); List<Map> listMaps = JSONArray. parseArray(result. toString(), Map. class); return listMaps; } // ------------------------------ test code ---------------- -------------------- public static void parseFormContentByJSONPath(String jsonDataStr) { // path = "$.d.results.formContents"; // path + = ".pmReviewContentDetail.competencySections.d.results"; // path + = ".competencies.d.results[0:5]"; JSONPath path = JSONPath.of("$.d.results[0:5].formContents"); // Cache and reuse can improve performance JSONReader parser = JSONReader.of(jsonDataStr); Object result = path. extract(parser); JSONArray customElementsArray = JSON. parseArray(result. toString()); List<FormContent> customElementsList = customElementsArray.toJavaList(FormContent.class); Logger.getLogger(loggerName).info(customElementsList.get(0).pmReviewContentDetail.formDataId); } public static void parseFormcustomElementJSONPath(String jsonDataStr) { String path = "$.d.results.formContents"; path + = ".pmReviewContentDetail.competencySections.d.results[*]"; // path + = ".competencies.d.results"; // path + = ".customElement.d.results"; // path + = ".elementListValues.d.results[0:5]"; JSONPath jsonPath = JSONPath.of(path); JSONReader parser = JSONReader.of(jsonDataStr); Object result = jsonPath. extract(parser); Logger.getLogger(loggerName).info(result.toString()); JSONArray customElementsArray = JSON. parseArray(result. toString()); List<FormCustomElement> customElementsList = customElementsArray.toJavaList(FormCustomElement.class); Logger.getLogger(loggerName).info(customElementsList.get(1).formContentId); } public static void parseJSONtoMap(String jsonDataStr) { String path = "$.d.results[0].formContents"; path + = ".pmReviewContentDetail"; // path + = ".competencies.d.results"; // path + = ".customElement.d.results"; // path + = ".elementListValues.d.results[0:5]"; JSONPath jsonPath = JSONPath.of(path); JSONReader parser = JSONReader.of(jsonDataStr); Object result = jsonPath. extract(parser); Logger.getLogger(loggerName).info(result.toString()); Map<String, Object> maps = JSONObject. parseObject(result. toString(), Map. class); Logger.getLogger(loggerName).info(maps.get("formContentId").toString()); Logger.getLogger(loggerName).info(maps.get("competencySections").toString()); } public static void parseJSONByClass(String jsonDataStr) { String path = "$.d.results[*]"; JSONPath jsonPath = JSONPath.of(path); JSONReader parser = JSONReader.of(jsonDataStr); Object result = jsonPath. extract(parser); // Logger.getLogger(loggerName).info(result.toString()); // JSONArray jsonArray = JSON. parseArray(result. toString()); List<Map> listMaps = JSONArray. parseArray(result. toString(), Map. class); listMaps.forEach(dataMap -> { dataMap.forEach((k, v) -> { // JSONObject data = JSON. parseObject(v. toString()); Logger.getLogger(loggerName).info(k + " -->" + v); }); }); } public static void parseFormcustomElementByMap(String jsonDataStr) { String path = "$.d.results.formContents"; path + = ".pmReviewContentDetail.competencySections.d.results[*]"; // path + = ".competencies.d.results"; // path + = ".customElement.d.results"; // path + = ".elementListValues.d.results[0:5]"; JSONPath jsonPath = JSONPath.of(path); JSONReader parser = JSONReader.of(jsonDataStr); Object result = jsonPath. extract(parser); Logger.getLogger(loggerName).info(result.toString()); List<Map> listMaps = JSONArray. parseArray(result. toString(), Map. class); listMaps.forEach(dataMap -> { dataMap.forEach((k, v) -> { // JSONObject data = JSON. parseObject(v. toString()); Logger.getLogger(loggerName).info(k + " -->" + v); }); }); } }