【java】java使用fastjson遍历jsonObject 和 jsonArray

public static void parseJson(Object object) {
    if(object instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) object;
        for (Map.Entry<String, Object> entry: jsonObject.entrySet()) {
            System.out.println(entry.getKey());
            Object o = entry.getValue();
            if(o instanceof String) {
                System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
            } else {
                parseJson(o);
            }
        }
    }
    if(object instanceof JSONArray) {
        JSONArray jsonArray = (JSONArray) object;
        for(int i = 0; i < jsonArray.size(); i ++) {
            parseJson(jsonArray.get(i));
        }
    }
    if(object instanceof String || object instanceof Integer) {
        System.out.println(object.toString());
    }

}

你可能感兴趣的:(java,java,算法,fastjson,jsonobject,jsonArray)