获取给定json字符串的各个值的数据类型

解析json


功能解析给定json,并把键key与其类型以下划线分割罗列出来

对JSON处理使用的工具是hutool的JSONUtil
相关依赖:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>4.1.19</version>
</dependency>

代码实现

//结果是以Map的形式展现
public static Map<String, String> getResult(JSONObject jsonObject) {
     
    Map<String, String> result = new HashMap<>();
    //遍历该json对象,通过键值name 来获取相应的json
    for (String name : jsonObject.keySet()) {
     
        result = getMap(name, name, jsonObject, result);
    }

    return result;
}


//递归获取所需map
//分为三种情况:1,该json节点下级为数组 2,该json节点下级为map 3,该json节点为终结点
//参数分析; name:json键的名称,key最终map键的名称,jsonObject当前节点的json对象,result:最终结果集
private static Map<String, String> getMap(String name, String key, JSONObject jsonObject, Map<String, String> result) {
     
    Object obj = jsonObject.get(name);
    if (JSONUtil.isJson(obj.toString())) {
     
        //最外层添加
        result.put(key, getType(obj));
        //是否还有下级
        if (JSONUtil.isJsonArray(obj.toString())) {
     
            //数组
            putJsonArray(obj,key,result);
        } else {
     
            //map
            putJsonObject(obj, key, result);
        }
    } else {
     
        //无下级直接存
        result.put(key, getType(obj));
    }
    return result;
}

private static void putJsonArray(Object obj ,String key, Map<String, String> result) {
     
    JSONArray jsonArray = JSONUtil.parseArray(obj.toString());
    String type = getType(jsonArray.get(0));
    if (type.equals("object")) {
     
        result.put(key, "List<" + type + ">");
        JSONObject jsonArrayObject = (JSONObject) jsonArray.get(0);
        //遍历集合的第一个元素的键
        for (String str : jsonArrayObject.keySet()) {
     
            String newName = "";
            newName = key + "_" + str;
            //递归调用
            getMap(str, newName, (JSONObject) jsonArray.get(0), result);
        }
    } else {
     
        result.put(key, "List<" + type + ">");
    }
}


private static void putJsonObject(Object obj, String key, Map<String, String> result) {
     
    JSONObject json = JSONUtil.parseObj(obj);
    //遍历当前json的所有键
    for (String str : json.keySet()) {
     
        String newName = "";
        newName = key + "_" + str;
        result.put(newName, getType(json.getObj(str)));
        //递归调用
        getMap(str, newName, json, result);
    }
}

//通过换取具体返回值的类来确定类型
private static String getType(Object obj) {
     
    Class<?> aClass = obj.getClass();
    String type = "";
    if (aClass.equals(Integer.class)) {
     
        type = "int";
    } else if (aClass.equals(Double.class)) {
     
        type = "double";
    } else if (aClass.equals(JSONObject.class)) {
     
        type = "object";
    } else {
     
        type = "string";
    }
    return type;
}

示例json
{
“credit_card”: {
“credit_no”: 20,
“credit_repo”: 10,
“create_card_info”: [
{
“credit_amt”: 1000.22,
“credit_status”: 0,
“credit_no”: 100001,
“credit_name”: “工商信用卡”
},
{
“credit_amt”: 1000.22,
“credit_status”: 0,
“credit_no”: 100001,
“credit_name”: “建行信用卡”
},
{
“credit_amt”: 1000.22,
“credit_status”: 0,
“credit_no”: 100001,
“credit_name”: “招商信用卡”
},
{
“credit_amt”: 1000.22,
“credit_status”: 0,
“credit_no”: 100001,
“credit_name”: “农行信用卡”
}
]
},
“duebill_card”: {
“duebill_card_info”: [
{
“duebill_name”: “工商信用卡”,
“duebill_status”: 0,
“duebill_amt”: 1000.22,
“duebill_card_no”: 100001
},
{
“duebill_name”: “工商信用卡”,
“duebill_card_no”: 100001,
“duebill_status”: 0,
“duebill_amt”: 1000.22
}
],
“duebill_no”: 10,
“duebill_repo”: 2
},
“ceshi”:[
“abc”,“def”
],
“reportsn”: 10001
}

结果获取给定json字符串的各个值的数据类型_第1张图片

你可能感兴趣的:(json)