FastJSON解析PMGM Entity Demo

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 competencySectionsPath = "$.d.results[*]";
        competencySectionsPath += ".pmReviewContentDetail";
        competencySectionsPath += ".competencySections.d.results[*]";
        List<Map> competencySectionsListMap = 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 competencySectionsListMap:" + competencySectionsListMap.size());
        System.out.println("size of competenciesListMap:" + competenciesListMap.size());
 
        // 需求:把level3的字段行转列
        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 = competencySectionsListMap.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"); // 缓存起来重复使用能提升性能
 
        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);
            });
        });
 
    }
 
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值