• 获取JSON中所有的KEY


    采用递归的方式,遍历JSON中所有的KEY。
    
    JSON格式如下:
    
    {"username":"tom","age":18,"address":[{"province":"上海市"},{"city":"上海市"},{"disrtict":"静安区"}]}
    
    处理后,得到的结果如下:
    
    address|province|city|disrtict|username|age
    
    package ai.advance.until;
    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.fastjson.JSONArray;
    
    public class fastJsonDiff {
        /**
         * 递归读取所有的key
         *
         * @param jsonObject
         */
        public static StringBuffer getAllKey(JSONObject jsonObject) {
            StringBuffer stringBuffer = new StringBuffer();
            Iterator<String> keys = jsonObject.keySet().iterator();// jsonObject.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                stringBuffer.append(key.toString()).append("|");
                if (jsonObject.get(key) instanceof JSONObject) {
                    JSONObject innerObject = (JSONObject) jsonObject.get(key);
                    stringBuffer.append(getAllKey(innerObject));
                } else if (jsonObject.get(key) instanceof JSONArray) {
                    JSONArray innerObject = (JSONArray) jsonObject.get(key);
                    stringBuffer.append(getAllKey(innerObject));
                }
            }
            return stringBuffer;
        }
    
        public static StringBuffer getAllKey(JSONArray json1) {
            StringBuffer stringBuffer = new StringBuffer();
            if (json1 != null ) {
                Iterator i1 = json1.iterator();
                while (i1.hasNext()) {
                    Object key = i1.next();
                    if (key instanceof  JSONObject) {
                        JSONObject innerObject = (JSONObject) key;
                        stringBuffer.append(getAllKey(innerObject));
                    } else if (key instanceof JSONArray) {
                        JSONArray innerObject = (JSONArray) key;
                        stringBuffer.append(getAllKey(innerObject));
                    }else{
                    }
                }
            }
            return stringBuffer;
        }
    
        private final static String st1 = "{"username":"tom","age":18,"address":[{"province":"上海市"},{"city":"上海市"},{"disrtict":"静安区"}]}";
        private final static String st2 = "{username:"tom",age:18}";
    
        public static void main(String[] args) {
            System.out.println(st1);
            JSONObject jsonObject1 = JSONObject.parseObject(st1);
            StringBuffer stb = getAllKey(jsonObject1);
            System.err.println(stb);
    
        }
    
    }
  • 相关阅读:
    bzoj 1098 [POI2007] 办公楼 biu
    「 Luogu P2574 」 XOR的艺术——线段树
    「 Luogu P2801 」 教主的魔法——分块
    Luogu P1438 无聊的数列
    「 Luogu P2420 」 让我们异或吧
    「 SPOJ GSS3 」 Can you answer these queries III
    「 HDOJ P2227 」 Find the nondecreasing subsequences
    「 HDOJ P3887 」 Counting Offspring
    一些性质及公式
    OI杂记
  • 原文地址:https://www.cnblogs.com/JonaLin/p/11040096.html
Copyright © 2020-2023  润新知