首先我使用的工具是json-lib
地址http://json-lib.sourceforge.net/
也可参考我的http://lyking2001.iteye.com/admin/blogs/504020了解json-lib
对于date类型的转化一直是json中比较复杂的事情,我这只提出一种自己会的方式与大家分享,如果还好更好的方式别忘记告诉我学习下嘿嘿在这先谢了
对于date类型直接转化如下
java.util.Date testDate = new Date(); JSONObject jsonFromDate = JSONObject.fromObject(testDate); System.out.println(jsonFromDate); //prints {"date":26,"day":1,"hours":11,"minutes":30,"month":9,"seconds":18,"time":1256527818296,"timezoneOffset":-480,"year":109}
上述转化后的格式明显不利于我们使用,下面介绍我的方法
//注册date类型的转化方式 JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImplTest()); JSONObject jsonFromBean = JSONObject.fromObject(testBean,jsonConfig); System.out.println(jsonFromBean); //prints {"birthday":"2009-10-26","id":"id","name":"name"} String[] dateFormats = new String[] {"yyyy/MM/dd","yyyy-MM-dd"}; JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats)); TestBean jsonToBean = (TestBean)JSONObject.toBean(jsonFromBean,TestBean.class); System.out.println(jsonToBean); //prints TestBean@1126b07[id=id,name=name,birthday=Mon Oct 26 00:00:00 CST 2009]
其中需要的类如下:
1.准备测试数据
import java.util.Date; import org.apache.commons.lang.builder.ReflectionToStringBuilder; public class TestBean { private String id; private String name; private java.util.Date birthday; public TestBean() { super(); } public TestBean(String id, String name, Date birthday) { super(); this.id = id; this.name = name; this.birthday = birthday; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public java.util.Date getBirthday() { return birthday; } public void setBirthday(java.util.Date birthday) { this.birthday = birthday; } public String toString() { return ReflectionToStringBuilder.toString(this); } }
2.创建date格式化类
import java.text.SimpleDateFormat; import java.util.Date; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; public class JsonValueProcessorImplTest implements JsonValueProcessor { private String format = "yyyy-MM-dd"; public JsonValueProcessorImplTest() { super(); } public JsonValueProcessorImplTest(String format) { super(); this.format = format; } @Override public Object processArrayValue(Object value, JsonConfig jsonConfig) { String[] obj = {}; if (value instanceof Date[]) { SimpleDateFormat sf = new SimpleDateFormat(format); Date[] dates = (Date[]) value; obj = new String[dates.length]; for (int i = 0; i < dates.length; i++) { obj[i] = sf.format(dates[i]); } } return obj; } @Override public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { if (value instanceof java.util.Date) { String str = new SimpleDateFormat(format).format((Date) value); return str; } return value.toString(); } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } }
通过如上方式我们就可以把含date类型的javabean在javabean和json格式中相互转化了