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
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 String concatJson(List list,boolean alreadyElementJson){
if(list==null)
return null;
StringBuilder sb=new StringBuilder();
sb.append("[");
int size=list.size();
for(int i=0; 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 String concatJson(List 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
sb.append(objectToJson(jsonObjects.get(i))+",");
}
if(sb.charAt(sb.length()-1)==','){
sb.deleteCharAt(sb.length()-1);
}
sb.append("]");
return sb.toString();
}
public static 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();
}