java 处理 简单对象的json格式互转
这个有点模仿于。google的json处理。
传进对象;传出json。
传进json;传出对象。
对于基本的简单对象例如:student
/** * Student * * @author shanzhu * @version 1.0 2011-10-17 */package json;import java.io.Serializable;public class Student implements Serializable { /** * */ private static final long serialVersionUID = 8424716517986346222L; private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
/** * json test * * @author shanzhu * @version 1.0 2011-10-17 */package json;import json.reflect.JsonReflect;import net.sf.json.JSONObject;public class JsonText { /** * @param args */ public static void main(String[] args) {// 对象转化成json格式的 // Student student = new Student();// student.setAge(10);// student.setId(1);// student.setName("bushyou");// Map mapObject = (new JsonReflect<Student>(student)).Class2JsonObject();// JSONObject jsonObject = new JSONObject();// jsonObject.put("status", 200);// jsonObject.put("data", mapObject);// System.out.println(jsonObject.toString()); // json格式转化成对象的 String strObject = "{"status":200,"data":{"id":1,"age":10,"name":"bushyou"}}"; //生成json格式 JSONObject jsonObject1 = JSONObject.fromObject(strObject); JSONObject jsonObject2 = (JSONObject) jsonObject1.get("data"); Student student = new Student(); student = new JsonReflect<Student>(student).JsonObject2Class(jsonObject2); System.out.println(student.getName()); }}
/** * 操作json格式的基础类 * * @author shanzhu * @version 1.0 2011-10-17 */package json.reflect;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;import json.reflect.factory.IType;import json.reflect.factory.JavaTypeFactory;import net.sf.json.JSONObject;public class JsonReflect<T> { private static final String SERIAL_VERSION_UID = "serialVersionUID"; public Class classType; private T t; public JsonReflect(T t) { this.t = t; classType = t.getClass(); } /** * 将类转化成json对象 * @return */ public Map Class2JsonObject() { Field[] fields = this.classType.getDeclaredFields(); String filedsName = ""; Object filedsValue = ""; Map map = new HashMap(); Field field = null; for (int i = 0; i < fields.length; i++) { field = fields[i]; field.setAccessible(true); filedsName = field.getName(); if (SERIAL_VERSION_UID.equalsIgnoreCase(filedsName)) { continue; } try { filedsValue = field.get(t); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } map.put(filedsName,filedsValue); field = null; } return map; } public T JsonObject2Class(JSONObject jsonObject){ T t = null; Object obj = null; try { obj = classType.newInstance(); } catch (InstantiationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }//通过class类反射一个对象实体 Field[] fields = this.classType.getDeclaredFields(); String filedsName = ""; Field field = null; for (int i = 0; i < fields.length; i++) { field = fields[i]; field.setAccessible(true); filedsName = field.getName(); if (SERIAL_VERSION_UID.equalsIgnoreCase(filedsName)) { continue; } String type = field.getType().getSimpleName(); //原始类型 IType typeObject =JavaTypeFactory.createType(type); Object object = typeObject.createData(jsonObject.get(filedsName)+""); //生成的值。。用于反射 Class classType = typeObject.getClassType(); //生成的类型。。用于反射 try { String filedsSet ="set" + filedsName.substring(0,1).toUpperCase()+ filedsName.substring(1); Method m = this.classType.getDeclaredMethod(filedsSet,new Class[]{classType}); m.invoke(obj,object); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } t = (T) obj; return t; }}
/** * java 基本类型生成工厂方法 * * @author shanzhu * @version 1.0 2011-10-17 */package json.reflect.factory;public class JavaTypeFactory { public static IType createType(String type){ if (type==null || "".equals(type)) { new IllegalAccessException("type is not can be null"); } if ("int".equalsIgnoreCase(type) || "Integer".equalsIgnoreCase(type)) { return new IntergetType(); } if ("String".equalsIgnoreCase(type)) { return new StringType(); } if ("date".equalsIgnoreCase(type)) { return new DateType(); } return new StringType(); }}
/** * Integer * * @author shanzhu * @version 1.0 2011-10-17 */package json.reflect.factory;public class IntergetType implements IType { @Override public Object createData(String oldValue) { // TODO Auto-generated method stub return new Integer(oldValue); } @Override public Class getClassType() { // TODO Auto-generated method stub return Integer.class; }}
/** * Date * * @author shanzhu * @version 1.0 2011-10-17 */package json.reflect.factory;import java.util.Date;public class DateType implements IType { @Override public Object createData(String oldValue) { // TODO Auto-generated method stub return new Date(oldValue); } @Override public Class getClassType() { // TODO Auto-generated method stub return Date.class; }}
/** * Long * * @author shanzhu * @version 1.0 2011-10-17 */package json.reflect.factory;public class LongType implements IType { @Override public Object createData(String oldValue) { return new Long(oldValue); } @Override public Class getClassType() { // TODO Auto-generated method stub return Long.class; }}
/** * String * * @author shanzhu * @version 1.0 2011-10-17 */package json.reflect.factory;public class StringType implements IType{ public String createData(String oldValue) { return new String(oldValue) ; } @Override public Class getClassType() { // TODO Auto-generated method stub return String.class; }}
/** * 接口 * * @author shanzhu * @version 1.0 2011-10-17 */package json.reflect.factory;public interface IType { //设置数据 public Object createData(String oldValue); public Class getClassType();}