将json字符串递归解析成类列表

将json字符串递归解析成类列表

  • 需求
  • 上代码
    • 解析器类
    • 类型枚举类
    • 实体
    • 测试类
  • 打完收功

需求

将json字符串的所有层都解析出来,得到每一层的信息,包括:层级、每层的key(如果是array类型,key是上一层的key)、每层的值、当前类型(JSONObject/JSONArray/Other)、父key、父类型 (JSONObject/JSONArray/Other)。

上代码

解析器类

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class JSONParser {

    public List<JSONEntity> analysisJSON(String data) {
        List<JSONEntity> jsonEntities = new ArrayList<>();
        this.analysisJSON(data, jsonEntities, null, null, null, 0);
        return jsonEntities;
    }

    public void analysisJSON(String value,
                             List<JSONEntity> jsonEntities,
                             String key,
                             JSONType parentType,
                             String parentKey,
                             Integer level) {

        JSONType jsonType = this.typeJudge(value);

        switch (jsonType) {
            case JSON_OBJECT:
                JSONObject jsonObject = JSON.parseObject(value);
                jsonObject.forEach((sunKey, sunValue) ->
                        this.analysisJSON(String.valueOf(sunValue), jsonEntities, sunKey, jsonType, key, level + 1));
                break;
            case JSON_ARRAY:
                JSONArray objects = JSON.parseArray(value);
                objects.forEach(sunValue ->
                        this.analysisJSON(String.valueOf(sunValue), jsonEntities, null, jsonType, key, level + 1));
                break;
        }

        JSONEntity jsonEntity = new JSONEntity();
        jsonEntity.setLevel(level);
        jsonEntity.setKey(key);
        jsonEntity.setParentType(parentType);
        jsonEntity.setValue(value);
        jsonEntity.setType(jsonType);
        jsonEntity.setParentKey(parentKey);
        jsonEntities.add(jsonEntity);
    }

    private JSONType typeJudge(String param) {
        Object parse;
        try {
            parse = JSON.parse(param);
        } catch (Exception e) {
            return JSONType.OTHER;
        }
        if (parse instanceof JSONObject) {
            return JSONType.JSON_OBJECT;
        } else if (parse instanceof JSONArray) {
            return JSONType.JSON_ARRAY;
        } else {
            return JSONType.OTHER;
        }
    }
}

类型枚举类

public enum JSONType {
    JSON_OBJECT,
    JSON_ARRAY,
    OTHER
}

实体

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class JSONEntity {
    /**
     * 层级
     */
    private Integer level;
    /**
     * key
     */
    private String key;
    /**
     * 每层的值
     */
    private String value;
    /**
     * 当前类型 JSONObject/JSONArray/Other
     */
    private JSONType type;
    /**
     * 父类型 JSONObject/JSONArray/Other
     */
    private JSONType parentType;
    /**
     * 父key
     */
    private String parentKey;
}

测试类

import java.util.Comparator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
//        String params = "[{\n" +
//                "\t\"flag\": \"2\",\n" +
//                "\t\"tea_id\": \"2002\",\n" +
//                "\t\"students\": [{\n" +
//                "\t\t\"stu_id\": \"1003\",\n" +
//                "\t\t\"stu_name\": \"zhangsan\"\n" +
//                "\t}, {\n" +
//                "\t\t\"stu_id\": \"1004\",\n" +
//                "\t\t\"stu_name\": \"李四\"\n" +
//                "\t}],\n" +
//                "\t\"tea_name\": \"王五\",\n" +
//                "\t\"haha\": [890, 678],\n" +
//                "\t\"test\": {\n" +
//                "\t\t\"l2\": \"qqq\",\n" +
//                "\t\t\"ww\": {\n" +
//                "\t\t\t\"l3\": \"v3\"\n" +
//                "\t\t}\n" +
//                "\t}\n" +
//                "}, {\n" +
//                "\t\"flag\": \"1\",\n" +
//                "\t\"tea_id\": \"2001\",\n" +
//                "\t\"students\": [{\n" +
//                "\t\t\"stu_id\": \"1001\",\n" +
//                "\t\t\"stu_name\": \"十一郎\"\n" +
//                "\t}, {\n" +
//                "\t\t\"stu_id\": \"1002\",\n" +
//                "\t\t\"stu_name\": \"十二郎\"\n" +
//                "\t}],\n" +
//                "\t\"tea_name\": \"晓春\",\n" +
//                "\t\"haha\": [123, 456],\n" +
//                "\t\"test\": {\n" +
//                "\t\t\"l2\": \"qqq\",\n" +
//                "\t\t\"ww\": {\n" +
//                "\t\t\t\"l3\": \"v3\"\n" +
//                "\t\t}\n" +
//                "\t}\n" +
//                "}, {\n" +
//                "\t\"a\": [1, 2, 3],\n" +
//                "\t\"b\": {\n" +
//                "\t\t\"flag\": \"2\",\n" +
//                "\t\t\"tea_id\": \"2002\",\n" +
//                "\t\t\"students\": [{\n" +
//                "\t\t\t\"stu_id\": \"1003\",\n" +
//                "\t\t\t\"stu_name\": \"zhangsan\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"stu_id\": \"1004\",\n" +
//                "\t\t\t\"stu_name\": \"李四\"\n" +
//                "\t\t}],\n" +
//                "\t\t\"tea_name\": \"王五\",\n" +
//                "\t\t\"haha\": [890, 678],\n" +
//                "\t\t\"test\": {\n" +
//                "\t\t\t\"l2\": \"qqq\",\n" +
//                "\t\t\t\"ww\": {\n" +
//                "\t\t\t\t\"l3\": \"v3\"\n" +
//                "\t\t\t}\n" +
//                "\t\t}\n" +
//                "\t}\n" +
//                "}]";

        String params = "{\n" +
                "\t\"a\": [1, 2, 3],\n" +
                "\t\"b\": {\n" +
                "\t\t\"flag\": \"2\",\n" +
                "\t\t\"tea_id\": \"2002\",\n" +
                "\t\t\"students\": [{\n" +
                "\t\t\t\"stu_id\": \"1003\",\n" +
                "\t\t\t\"stu_name\": \"zhangsan\"\n" +
                "\t\t}, {\n" +
                "\t\t\t\"stu_id\": \"1004\",\n" +
                "\t\t\t\"stu_name\": \"李四\"\n" +
                "\t\t}],\n" +
                "\t\t\"tea_name\": \"王五\",\n" +
                "\t\t\"haha\": [890, 678],\n" +
                "\t\t\"test\": {\n" +
                "\t\t\t\"l2\": \"qqq\",\n" +
                "\t\t\t\"ww\": {\n" +
                "\t\t\t\t\"l3\": \"v3\"\n" +
                "\t\t\t}\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}";
        System.out.println("params=" + params);
        JSONParser parser = new JSONParser();
        List<JSONEntity> jsonEntities = parser.analysisJSON(params);
        jsonEntities.stream().sorted(Comparator.comparing(JSONEntity::getLevel)).forEach(System.out::println);
    }
}

打完收功

你可能感兴趣的:(java,json)