java解析大json包的两种方式

方式一:使用gson-2.8.6.jar包

package com.utils;

import com.bzl.model.vo.CustomEngineObjectVO;
import com.entity.CustomEngineObject;
import com.entity.EngineObject;
import com.google.gson.stream.JsonReader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;

/**
 * 解析自定义属性
 *
 * @author
 * @date 2022/6/27 17:45
 **/
@Component
@Slf4j
public class AnalysisPropertiesUtil {

    /**
     * 解析自定义属性
     *
     * @param customerPropertyUrlGz
     * @return
     * @throws Exception
     */
    private void analysisCustomProperties(String customerPropertyUrlGz) throws Exception {
        URL url = new URL(customerPropertyUrlGz);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.connect();
        InputStream in = conn.getInputStream();
        GZIPInputStream gzin = new GZIPInputStream(in);
        InputStreamReader inputStreamReader = new InputStreamReader(gzin, "UTF-8");
        //进行转换
        parseJson(inputStreamReader);
    }

    private void parseJson(InputStreamReader inputStreamReader) throws IOException {
        JsonReader reader = new JsonReader(inputStreamReader);
        reader.beginObject();
        CustomEngineObjectVO customEngineObject = new CustomEngineObjectVO();
        while (reader.hasNext()) {
            switch (reader.nextName()) {
                case "fileId":
                    customEngineObject.setFileId(reader.nextString());
                    break;
                case "modelPropertyDTOList":
                    parseModelPropertyDTOList(reader);
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();
        reader.close();
    }

    private void parseModelPropertyDTOList(JsonReader reader) throws IOException {
        reader.beginArray();
        CustomEngineObject customEngineObject001;
        while (reader.hasNext()) {
            customEngineObject001 = new CustomEngineObject();
            reader.beginObject();
            while (reader.hasNext()) {
                switch (reader.nextName()) {
                    case "propertySets":
                        parsePropertySets(reader, customEngineObject001);
                        break;
                    case "uuid":
                        customEngineObject001.setUuid(reader.nextString());
                        break;
                    default:
                        reader.skipValue();
                        break;
                }
            }

            reader.endObject();
        }
        reader.endArray();
    }

    private void parsePropertySets(JsonReader reader, CustomEngineObject customEngineObject001) throws IOException {
        reader.beginArray();
        List propertySets = new ArrayList();
        EngineObject.PropertySet propertySet;
        while (reader.hasNext()) {
            propertySet = new EngineObject.PropertySet();
            reader.beginObject();
            while (reader.hasNext()) {
                switch (reader.nextName()) {
                    case "name":
                        propertySet.setName(reader.nextString());
                        break;
                    case "properties":
                        List properties = new ArrayList<>();
                        parseProperties(reader, properties);
                        propertySet.setProperties(properties);
                        break;
                    default:
                        reader.skipValue();
                }
            }
            propertySets.add(propertySet);
            reader.endObject();
        }
        reader.endArray();
        customEngineObject001.setPropertySets(propertySets);
    }

    private void parseProperties(JsonReader reader, List properties) throws IOException {
        EngineObject.Property property;
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginObject();
            property = new EngineObject.Property();
            while (reader.hasNext()) {
                switch (reader.nextName()) {
                    case "name":
                        property.setName(reader.nextString());
                        break;
                    case "value":
                        property.setValue(reader.nextString());
                        break;
                    default:
                        reader.skipValue();
                }
            }
            reader.endObject();
            properties.add(property);
        }
        reader.endArray();
    }

}

方式二:使用jackson-core-2.11.2.jar

package com.integration.utils;

import com.integration.feign.bean.ModelProperties;
import com.integration.feign.service.BtpChangeUrlFeignClientService;
import com.bzl.bimdc.exceptions.BimdcException;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.MappingJsonFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.zip.GZIPInputStream;

/**
 * 解析固有属性信息
 *
 * @author
 * @date 2022/6/9 17:45
 **/
@Component
@Slf4j
public class AnalysisFixedPropertiesUtil {
    @Resource
    private BtpChangeUrlFeignClientService btpChangeUrlFeignClientService;

    /**
     * 解析固有属性
     *
     * @param propertyJsonUrlGz
     * @return
     * @throws Exception
     */
    public void analyzeFixedProperties(String propertyJsonUrlGz) {
        File originPropertyFile = null;
        try {
            //下载固有属性
            log.info("获取图形引擎固有属性数据成功,固有属性链接为:{}", propertyJsonUrlGz);
            originPropertyFile = downloadProperty(propertyJsonUrlGz);

            if (originPropertyFile == null) {
                log.error("固有属性文件为空");
                return;
            }
            log.info("AnalysisFixedPropertiesUtil-analyzeFixedProperties-start:{}", System.currentTimeMillis());
            JsonFactory f = new MappingJsonFactory();
            JsonParser parser = f.createJsonParser(originPropertyFile);
            JsonToken currentJsonToken;
            while (!parser.isClosed()) {
                currentJsonToken = parser.nextToken();
                while (!JsonToken.END_ARRAY.equals(currentJsonToken) && !parser.isClosed()) {

                    if (JsonToken.START_OBJECT.equals(currentJsonToken)) {
                        parseOriginProperty(parser, currentJsonToken);
                    }
                    currentJsonToken = parser.nextToken();
                }
            }
            //删除属性文件
            originPropertyFile.delete();
            log.info("AnalysisFixedPropertiesUtil-analyzeFixedProperties-end:time={}", System.currentTimeMillis());
        } catch (Exception e) {
            if (null != originPropertyFile) {
                //删除属性文件
                originPropertyFile.delete();
            }
            log.error("解析固有属性失败:", e);
            throw new BimdcException(HttpStatus.BAD_REQUEST.value(), "解析固有属性失败");
        }
    }

    private void parseOriginProperty(JsonParser parser, JsonToken currentJsonToken) throws IOException {
        String key = null;
        String originPropertyUuid = null;
        ModelProperties modelProperties = new ModelProperties();
        while (!JsonToken.END_OBJECT.equals(currentJsonToken) && !parser.isClosed()) {

            if (JsonToken.FIELD_NAME.equals(currentJsonToken)) {
                key = parser.getCurrentName();
                currentJsonToken = parser.nextToken();
                if ("uuid".equals(key)) {
                    originPropertyUuid = parser.getValueAsString();
                    //设置UUID
                    modelProperties.setUuid(originPropertyUuid);
                } else if ("propertySets".equals(key)) {
                    //获取属性集
                    parsePropertySets(parser, currentJsonToken, modelProperties);
                } else if ("location".equals(key)) {
                    ModelProperties.Location location = new ModelProperties.Location();
                    //获取标高
                    parseLocation(parser, currentJsonToken, location);
                    modelProperties.setLocation(location);
                } else {
                    ignore(parser, currentJsonToken);
                }
            } else {
                currentJsonToken = parser.nextToken();
            }
        }
    }

    private void parsePropertySets(JsonParser parser, JsonToken currentJsonToken, ModelProperties modelProperties) throws IOException {
        String key = null;
        List propertySets = new ArrayList<>();
        ModelProperties.PropertySet propertySet;
        while (!JsonToken.END_ARRAY.equals(currentJsonToken) && !parser.isClosed()) {
            if (JsonToken.START_OBJECT.equals(currentJsonToken)) {
                propertySet = new ModelProperties.PropertySet();
                while (!JsonToken.END_OBJECT.equals(currentJsonToken) && !parser.isClosed()) {

                    if (JsonToken.FIELD_NAME.equals(currentJsonToken)) {
                        currentJsonToken = parser.nextToken();
                        key = parser.getCurrentName();
                        if ("name".equals(key)) {
                            propertySet.setName(parser.getValueAsString());
                        } else if ("properties".equals(key)) {
                            //属性集合
                            List properties = new ArrayList<>();
                            //解析属性
                            parseProperties(parser, currentJsonToken, properties);
                            propertySet.setProperties(properties);
                        } else {
                            ignore(parser, currentJsonToken);
                        }

                    }
                    currentJsonToken = parser.nextToken();
                }
                propertySets.add(propertySet);
            } else {
                currentJsonToken = parser.nextToken();
            }
        }
        modelProperties.setPropertySets(propertySets);
    }

    private void parseProperties(JsonParser parser, JsonToken currentJsonToken, List properties) throws IOException {

        while (!JsonToken.END_ARRAY.equals(currentJsonToken) && !parser.isClosed()) {
            if (JsonToken.START_OBJECT.equals(currentJsonToken)) {
                parseProperty(parser, currentJsonToken, properties);
            }
            currentJsonToken = parser.nextToken();
        }
    }

    private void parseProperty(JsonParser parser, JsonToken currentJsonToken, List properties) throws IOException {
        String key = null;
        ModelProperties.Properties property = new ModelProperties.Properties();
        while (!JsonToken.END_OBJECT.equals(currentJsonToken) && !parser.isClosed()) {
            if (JsonToken.FIELD_NAME.equals(currentJsonToken)) {
                key = parser.getCurrentName();
                currentJsonToken = parser.nextToken();

                switch (key) {
                    case "name":
                        property.setName(parser.getValueAsString());
                        break;
                    case "value":
                        property.setValue(parser.getValueAsString());
                        break;
                    default:
                        ignore(parser, currentJsonToken);
                        break;
                }
            } else {
                currentJsonToken = parser.nextToken();
            }
        }
        properties.add(property);
    }

    private void parseLocation(JsonParser parser, JsonToken currentJsonToken, ModelProperties.Location location) throws IOException {
        String key = null;
        while (!JsonToken.END_OBJECT.equals(currentJsonToken) && !parser.isClosed()) {
            if (JsonToken.FIELD_NAME.equals(currentJsonToken)) {
                key = parser.getCurrentName();
                currentJsonToken = parser.nextToken();

                switch (key) {
                    case "uuid":
                        location.setUuid(parser.getValueAsString());
                        break;
                    case "name":
                        location.setName(parser.getValueAsString());
                        break;
                    default:
                        ignore(parser, currentJsonToken);
                        break;
                }
            } else {
                currentJsonToken = parser.nextToken();
            }
        }
    }

    private void ignore(JsonParser parser, JsonToken currentJsonToken) throws IOException {
        int arrNum = 0;
        int objNum = 0;
        if (JsonToken.START_ARRAY.equals(currentJsonToken)) {
            arrNum++;
        } else if (JsonToken.START_OBJECT.equals(currentJsonToken)) {
            objNum++;
        } else if (JsonToken.END_ARRAY.equals(currentJsonToken)) {
            arrNum--;
        } else if (JsonToken.END_OBJECT.equals(currentJsonToken)) {
            objNum--;
        }
        while (arrNum > 0 || objNum > 0) {
            currentJsonToken = parser.nextToken();
            if (JsonToken.START_ARRAY.equals(currentJsonToken)) {
                arrNum++;
            } else if (JsonToken.START_OBJECT.equals(currentJsonToken)) {
                objNum++;
            } else if (JsonToken.END_ARRAY.equals(currentJsonToken)) {
                arrNum--;
            } else if (JsonToken.END_OBJECT.equals(currentJsonToken)) {
                objNum--;
            }
        }
    }

    private File downloadProperty(String url) {
        //下载固有属性
        InputStream inputStream = btpChangeUrlFeignClientService.downFile(url);

        String fileName = "property." + UUID.randomUUID().toString().replaceAll("-", "");

        File gzFile = new File(fileName + ".gz");
        BufferedOutputStream fileOutputStream = null;
        try {

            fileOutputStream = new BufferedOutputStream(new FileOutputStream(gzFile));
            int num = -1;
            byte[] buf = new byte[1024];
            while ((num = inputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, num);
            }

            fileOutputStream.flush();
        } catch (FileNotFoundException e) {
            log.error("AnalysisFixedPropertiesUtil.downloadProperty.下载属性包报错1。e=", e);
        } catch (IOException e) {
            log.error("AnalysisFixedPropertiesUtil.downloadProperty.下载属性包报错2。e=", e);
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                log.error("AnalysisFixedPropertiesUtil.downloadProperty.关闭FileOutputStream流失败.e=", e);
            }
        }

        //解压 gz
        File jsonFile = new File(fileName + ".json");
        BufferedOutputStream fos = null;
        try {
            fos = new BufferedOutputStream(new FileOutputStream(jsonFile));
            GZIPInputStream gzin = new GZIPInputStream(new FileInputStream(gzFile));
            int num = -1;
            byte[] buf = new byte[1024];
            while ((num = gzin.read(buf)) != -1) {
                fos.write(buf, 0, num);
            }

            fos.flush();
            gzin.close();
        } catch (IOException e) {
            log.error("AnalysisFixedPropertiesUtil.downloadProperty.解压属性包失败.e=", e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    log.error("AnalysisFixedPropertiesUtil.downloadProperty.关闭 BufferedOutputStream 流失败.e=", e);
                }
            }
        }

        gzFile.delete();

        return jsonFile;
    }

}

你可能感兴趣的:(java,json,开发语言)