常⽤的java类型转json的转换类公司不准引⼊jar所以只好⾃⼰写了⼀个基本类型的object转json的类,然后⽀持8种基本类型和javabean,list,map,array,使⽤putJSON⽅法,key值随便写,value是要转换的对象。
JsonObjectConvert.java:
package com.pz.util;
import flect.Field;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class JsonObjectConvert {
private StringBuffer sf = null;
public  String SimpleObjectToJson(Object o){
sf = new StringBuffer("{");
//sf.Class().getSimpleName());
Field field[] = o.getClass().getDeclaredFields();
String content = "";
for(Field f : field ){
try {
f.setAccessible(true);
content += "\"" + f.getName() + "\":\"" + (o)) +"\",";
} catch (IllegalArgumentException e) {
/
/ TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sf.append(content);
String().substring(0, sf.length() - 1) + "}";
}
public Object convertNull(Object o){
if(o == null){
return "";
}else {
return o;
}
}
@SuppressWarnings("unchecked")
public  Object convertJSON(Object o){
if(o == null){
return "";
}else if(o instanceof Boolean){
return "\"" +o.toString() + "\"";
}else if(o instanceof Character){
return "\"" +o.toString() + "\"";
}else if(o instanceof Short){
return "\"" +o.toString() + "\"";
}else if(o instanceof Integer){
return "\"" +o.toString() + "\"";
return "\"" +o.toString() + "\"";
}else if(o instanceof Long){
return "\"" +o.toString() + "\"";
}else if(o instanceof Float){
return "\"" +o.toString() + "\"";json值的类型有哪些
}else if(o instanceof Double){
return "\"" +o.toString() + "\"";
}else if(o instanceof Byte){
return "\"" +o.toString() + "\"";
}else if(o instanceof List){
return listToJSON((List)o);
}else if(o instanceof Map){
return mapToJSON((Map)o);
}else Class().isArray()){
return arrayToJSON(o);
}else if(o instanceof String){
return "\"" +o.toString() + "\"";
}else if(o instanceof Date){
return "\"" +o.toString() + "\"";
}
return SimpleObjectToJson(o);
}
@SuppressWarnings("unchecked")
public  String listToJSON(List list){
sf = new StringBuffer("[");
for(int i = 0;i < list.size();i++){
sf.append( (i)) + ",");
}
String().substring(String().length() - 1) + "]";
}
@SuppressWarnings("unchecked")
public  String mapToJSON(Map map){
sf = new StringBuffer("[");
Iterator ite = Set().iterator();
while(ite.hasNext()){
Entry o = (();
String key = Key()).toString();
sf.append("{\"" + key.substring(1, key.length() - 1) + "\":");
sf.append(Value()) +"},");
}
String().substring(String().length() - 1) + "]";
}
public String arrayToJSON(Object o){
sf = new StringBuffer("[");
Object[] obj = (Object[])o;
for(int i = 0;i < obj.length;i++){
sf.append(convertJSON(obj[i]) + ",");
}
String().substring(0, sf.toString().length() - 1) + "]";
}
//the sum method convert to json key --> value
public static String putJSON(String key,Object value){
JsonObjectConvert joc = new JsonObjectConvert();
String jsonString = "{\"" + key + "\":" + vertJSON(value) + "}";
return jsonString;
}
}
package com.pz.util;
import flect.Field;import java.util.Date;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;
public class JsonObjectConvert {private StringBuffer sf = null;public  String SimpleObjectToJson(Object o){sf = new StringBuffer("{");//sf.Class().getSimpleName());Field field[] = o.getClass().getDeclaredFields();String content = "";for(Field f : field ){try {f.setAccessible(true);content += "\"" + f.getName() + "\":\"" + (o)) +"\",";} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();}}sf.append(content);String().substring(0, sf.length() - 1) + "}";}public Object convertNull(Object o){if(o == null){return "";}else {return o;}}@SuppressWarnings("unchecked")public
Object convertJSON(Object o){if(o == null){return "";}else if(o instanceof Boolean){return "\"" +o.toString() + "\"";}else if(o instanceof Character){return "\"" +o.toString() + "\"";}else if(o instanceof Short){return "\"" +o.toString() + "\"";}else if(o instanceof Integer){return "\"" +o.toString() + "\"";}else if(o instanceof Long){return "\"" +o.toString() + "\"";}else if(o instanceof Float){return "\"" +o.toString() + "\"";}else if(o instanceof Double){return "\"" +o.toString() + "\"";}else if(o instanceof Byte){return "\"" +o.toString() + "\"";}else if(o instanceof List){return listToJSON((List)o);}else if(o instanceof Map){return mapToJSON((Map)o);}else Class().isArray()){return arrayToJSON(o);}else if(o instance
of String){return "\"" +o.toString() + "\"";}else if(o instanceof Date){return "\"" +o.toString() + "\"";}return
SimpleObjectToJson(o);}@SuppressWarnings("unchecked")public  String listToJSON(List list){sf = new StringBuffer(" [");for(int i = 0;i < list.size();i++){sf.append( (i)) + ",");}return
"\":");sf.append(Value()) +"},");}String().substring(String().length() - 1) + "]";}public String arrayToJSON(Object o){sf = new StringBuffer("[");Object[] obj = (Object[])o;for(int i = 0;i < obj.length;i++)
{sf.append(convertJSON(obj[i]) + ",");}String().substring(0, sf.toString().length() - 1) + "]";}//the sum method convert to json key --> valuepublic static String putJSON(String key,Object value){JsonObjectConvert joc = new JsonObjectConvert();String jsonString = "{\"" + key + "\":" + vertJSON(value) + "}"; return jsonString;}}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。