JAVA--递归获取嵌套json中所有key

当我们遇到复杂嵌套json,不知道其中有哪些属性,但是又想取到每条数据的所有值,并且想要拼接key让数据mapping成(全路径名称:值)的方式,可以参照如下方式:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class JsonUtil {
    public static void main(String[] args) {
        String jsonStr = "{\"k8s_app_name\":\"roc\",\"json_extract\":{\"data\":{\"volume\":6,\"rcu\":{\"battery\":{\"temperature\":32,\"chargingStatus\":1,\"percent\":99}},\"robotStatus\":1,\"asrStatus\":1},\"serviceCode\":\"\",\"resultCode\":0,\"rcuId\":\"5013951A62CB\",\"source\":\"ROC\",\"robotId\":\"5013951A62CB\",\"resultInfo\":\"\",\"accountId\":\"AZ019179\",\"robotType\":\"patrol\",\"rodType\":\"reportMetrics\",\"tenantId\":\"patrol_jindi3\",\"guid\":\"9c20192006b845fd90714d5069b93d8b\",\"tag\":\"\",\"moduleId\":\"reportStatus\",\"timestamp\":\"2023-03-31T23:59:58.672+08:00\"},\"label\":\"bj-prod-221_roc_roc_roc-operation-service_harixaudit\",\"k8s_svc_name\":\"roc-operation-service\",\"k8s_env_name\":\"bj-prod-221\",\"output\":\"eskafka\"}";

        HashMap<String, Object> map = new HashMap<>();
        Map<String, Object> resultMap = analysisJson(jsonStr, "", map);

        Iterator<String> iterator = resultMap.keySet().iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println("result_key: " + next);
        }
    }

    public static Map<String, Object> analysisJson(String jsonStr, String flag, Map<String, Object> jsonMap) {
        try {
            JSONObject jsonObject = JSON.parseObject(jsonStr);
            Iterator it = jsonObject.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next().toString();
                Object object = jsonObject.get(key);
                if (null != object && isJson(object.toString())) {//如果key中是一个json对象
                    String path = "";
                    if (StringUtils.isNotBlank(flag)) {
//                    path = flag + "." + key;
                        path = flag + "_" + key.replaceAll("\\$", "_D_");
                    } else {
                        path = key.replaceAll("\\$", "_D_");
                    }
//                    jsonMap.put(path, object);
                    analysisJson(object.toString(), path, jsonMap);
                } else {//如果key中是其他
                    String path = "";
                    if (StringUtils.isNotBlank(flag)) {
//                    path = flag + "." + key;
                        path = flag + "_" + key.replaceAll("\\$", "_D_");
                    } else {
                        path = key.replaceAll("\\$", "_D_");
                    }
                    System.out.println(path + ":" + object.toString() + " ");
                    jsonMap.put(path, object);
                }
            }

        } catch (Exception ignored) {

        }
        return jsonMap;
    }

    public static boolean isJson(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        Object parse = null;
        try {
            parse = JSON.parse(str);
        } catch (Exception e) {
            return false;
        }
        if (parse instanceof JSONObject) {
            return true;
        }
        return false;
    }
}

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