zoukankan      html  css  js  c++  java
  • 递归获取jsonObject的所有value

     //递归获取jsonObject的所有value
     private StringBuffer mStringBuffer = new StringBuffer();
     public  String getAllContentFromJson(Object cObject) {
    
            if(cObject instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) cObject;
                for (Map.Entry<String, Object> entry: jsonObject.entrySet()) {
                    Object o = entry.getValue();
                    if(o instanceof Integer){
                        log.info("key:" + entry.getKey() + ",value:" + entry.getValue());
                        mStringBuffer.append(" "+entry.getValue());
                    }else if(o instanceof Double){
                        log.info("key:" + entry.getKey() + ",value:" + entry.getValue());
                        mStringBuffer.append(" "+entry.getValue());
                    }else if(o instanceof Float){
                        log.info("key:" + entry.getKey() + ",value:" + entry.getValue());
                        mStringBuffer.append(" "+entry.getValue());
                    }else if(o instanceof Byte){
                        log.info("key:" + entry.getKey() + ",value:" + entry.getValue());
                        mStringBuffer.append(" "+entry.getValue());
                    }else if(o instanceof Long){
                        log.info("key:" + entry.getKey() + ",value:" + entry.getValue());
                        mStringBuffer.append(" "+entry.getValue());
                    }else if(o instanceof String) {
                        Object object = null;
                        try{
                            object=JSONObject.parseObject((String)o);
                            getAllContentFromJson(object);
                        }catch (Exception e){
                            log.info("key:" + entry.getKey() + ",value:" + entry.getValue());
                            mStringBuffer.append(" "+entry.getValue());
                        }
    
    
                    }
                    else {
                        getAllContentFromJson(o);
                    }
                }
            }
            if(cObject instanceof JSONArray) {
                JSONArray jsonArray = (JSONArray) cObject;
                for(int i = 0; i < jsonArray.size(); i ++) {
                    getAllContentFromJson(jsonArray.get(i));
                }
            }
            return mStringBuffer.toString();
        }
  • 相关阅读:
    需求分析-配置软件开发的出发点
    有关tab页的
    有关菜单的
    有关树形结构的
    需求分析-新闻发布的完整需求
    需求分析-网盘类的需求分析
    需求分析-有关有关富文本编辑器的需求
    Objective-C中的instancetype和id区别
    webservice远程调试开启
    Controller之间传递数据:Block传值
  • 原文地址:https://www.cnblogs.com/heyt/p/13574334.html
Copyright © 2011-2022 走看看