常用的java类型转json的转换类
公司不准引入json-lib-2.x.jar所以只好自己写了一个基本类型的object转json的类,然后支持8种基本类型和javabean,list,map,array,使用putJSON方法,key值随便写,value是要转换的对象。
JsonObjectConvert.java:
package com.pz.util; import java.lang.reflect.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.append(o.getClass().getSimpleName()); Field field[] = o.getClass().getDeclaredFields(); String content = ""; for(Field f : field ){ try { f.setAccessible(true); content += "\"" + f.getName() + "\":\"" + convertNull(f.get(o)) +"\","; } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } sf.append(content); return sf.toString().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 if(o.getClass().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( convertJSON(list.get(i)) + ","); } return sf.toString().substring(0,sf.toString().length() - 1) + "]"; } @SuppressWarnings("unchecked") public String mapToJSON(Map map){ sf = new StringBuffer("["); Iterator ite = map.entrySet().iterator(); while(ite.hasNext()){ Entry o = (Entry)ite.next(); String key = convertJSON(o.getKey()).toString(); sf.append("{\"" + key.substring(1, key.length() - 1) + "\":"); sf.append(convertJSON(o.getValue()) +"},"); } return sf.toString().substring(0,sf.toString().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]) + ","); } return sf.toString().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 + "\":" + joc.convertJSON(value) + "}"; return jsonString; } }
package com.pz.util;
import java.lang.reflect.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.append(o.getClass().getSimpleName());Field field[] = o.getClass().getDeclaredFields();String content = "";for(Field f : field ){try {f.setAccessible(true);content += "\"" + f.getName() + "\":\"" + convertNull(f.get(o)) +"\",";} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();}}sf.append(content);return sf.toString().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 if(o.getClass().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( convertJSON(list.get(i)) + ",");}return sf.toString().substring(0,sf.toString().length() - 1) + "]";}@SuppressWarnings("unchecked")public String mapToJSON(Map map){sf = new StringBuffer("[");Iterator ite = map.entrySet().iterator();while(ite.hasNext()){Entry o = (Entry)ite.next();String key = convertJSON(o.getKey()).toString();sf.append("{\"" + key.substring(1, key.length() - 1) + "\":");sf.append(convertJSON(o.getValue()) +"},");}return sf.toString().substring(0,sf.toString().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]) + ",");}return sf.toString().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 + "\":" + joc.convertJSON(value) + "}"; return jsonString;}}