原文链接: 示例代码:
JsonBean bean = new JsonBean();
bean.setName("NewBaby");
bean.setAge(1);
bean.setBorn(new Date());
jo = JSONObject.fromObject(bean);
System.out.println("bean->json:" +
jo.toString()); 打印结果:bean->json:{"age":1,"born":{"date":10,"day":3,"hours":14,"minutes":14,"month":2,"seconds":1,"time":1268201641228,"timezoneOffset":-480,"year":110},"name":"NewBaby"}
这时你会发现它把bean对象里的util.Date这个类型的所有属性一一转换出来。在实际运用过程中,大多数情况下我们希望能转化为yyyy-MM-dd这种格式,下面就讲一讲如何实现。 首先要写一个新的类JsonDateValueProcessor如下:
public class JsonDateValueProcessor implements JsonValueProcessor
{
private
String datePattern = "yyyy-MM-dd";
public
JsonDateValueProcessor() {
super();
}