• Javal连接字符串为Json


    public static  String concatJson(String[] keys,String[] values,String[] alreadyJsonKeys){
            if(keys==null|| values==null)
                return null;
            if(keys.length!=values.length)
                return "Json转换出错:key与values长度不相等";
            StringBuilder sb=new StringBuilder();
            sb.append("{");
            for(int i=0;i<keys.length;i++){
                Object object=values[i];
                if(object==null){
                    sb.append("""+ keys[i] +"":null,");
                }else{
                    if(alreadyJsonKeys!=null
                    &&CollectionsUtils.indexOf(alreadyJsonKeys,keys[i])>=0){
                        sb.append("""+ keys[i] +"":" + object + ",");
                    }else{
                        sb.append("""+ keys[i] +"":"" + object + "",");
                    }
                }
            }
    
            if(sb.charAt(sb.length()-1)==','){
                sb.deleteCharAt(sb.length()-1);
            }
            sb.append("}");
            return sb.toString();
        }
        public  static <T> String concatJson(List<T> list,boolean alreadyElementJson){
            if(list==null)
                return null;
            StringBuilder sb=new StringBuilder();
            sb.append("[");
            int size=list.size();
            for(int i=0; i<size;i++){
                T object=list.get(i);
                if(object==null){
                    sb.append("null");
                }
                if(object instanceof Date){
                    sb.append(""" + object + "",");
                }else if(object instanceof  String){
                    if(alreadyElementJson){  //已经是json字符串,不加引号
                        sb.append(object.toString()+",");
                    }else{
                        sb.append(""" + object + "",");
                    }
                }else if (object instanceof Boolean){
                    sb.append(object.toString() + ",");
                }else{
                    sb.append(objectToJson(object) + ",");
                }
            }
            if(sb.charAt(sb.length()-1)==','){
                sb.deleteCharAt(sb.length()-1);
            }
            sb.append("]");
            return sb.toString();
        }
    
        public static<T> String concatJson(List<T> jsonObjects){
            if(jsonObjects==null){
                return null;
            }
            if(jsonObjects.size()==0){
                return null;
            }
            StringBuilder sb=new StringBuilder();
            sb.append("[");
            int size=jsonObjects.size();
            for(int i=0;i<size;i++){
                sb.append(objectToJson(jsonObjects.get(i))+",");
            }
            if(sb.charAt(sb.length()-1)==','){
                sb.deleteCharAt(sb.length()-1);
            }
            sb.append("]");
            return sb.toString();
        }
        public static <T> String objectToJson(T t){
            if(t==null)
                return null;
            if(t instanceof List){
                return concatJson((List)t,false);
            }else if(t instanceof String ||t instanceof Date){
                return """+t.toString()+""";
            }else if( t instanceof Integer || t instanceof Long || t instanceof Double
                    || t instanceof BigDecimal || t instanceof Float
                    || t instanceof Boolean) {
                return t.toString() ; //字符串
            }
            return t.toString();
        }
    

      

  • 相关阅读:
    Android Animation 动画
    Android 发布Apk签名
    Android的长度单位及屏幕分辨率
    动态加载css、js引用
    Cassandra1.2文档学习(10)—— 插入和更新数据
    Cassandra1.2文档学习(9)—— 数据写入
    Cassandra1.2文档学习(8)—— 数据管理
    Cassandra1.2文档学习(7)—— 规划集群部署
    Cassandra1.2文档学习(6)—— 客户端数据请求
    Cassandra1.2文档学习(5)—— Snitch
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/10730704.html
Copyright © 2020-2023  润新知