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

解析json


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

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

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

代码实现

//结果是以Map<String,String>的形式展现
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
}

结果在这里插入图片描述

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值