标签:
Posted on 2015-02-14 09:58 RyanChan 阅读(61) 评论(0) 编辑 收藏
Json与Java Bean互相转换时,Bean中的Timestamp字段是无法直接处理的,需要实现两个转换器。
DateJsonValueProcessor的作用是Bean转换为Json时将Timepstamp转换为指定的时间格式。
1 import java.text.DateFormat;
2 import java.text.SimpleDateFormat;
3 import java.util.Date;
4 import net.sf.json.JsonConfig;
5 import net.sf.json.processors.JsonValueProcessor;
6 /*** 将Bean中的Timestamp转换为json中的日期字符串*/
7 public class DateJsonValueProcessor implements JsonValueProcessor {
8 public static final String Default_DATE_PATTERN ="yyyy-MM-dd";
9 private DateFormat dateFormat ;
10 public DateJsonValueProcessor(String datePattern){
11 try{
12 dateFormat = new SimpleDateFormat(datePattern);}
13 catch(Exception e ){
14 dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);
15 }
16 }
17 public Object processArrayValue(Object value, JsonConfig jsonConfig) {
18 return process(value);
19 }
20 public Object processObjectValue(String key, Object value,JsonConfig jsonConfig) {
21 return process(value);
22 }
23 private Object process(Object value){
24 return dateFormat.format((Date)value);
25 }
26 }
TimestampMorpher的作用则与DateJsonValueProcessor相反,它是在Jsonl转换为Bean时,会把指定的时间格式转换为Timestamp
1 import java.sql.Timestamp;
2 import java.text.ParseException;
3 import java.text.SimpleDateFormat;
4 import net.sf.ezmorph.MorphException;
5 import net.sf.ezmorph.object.AbstractObjectMorpher;
6 /*** 将json串中的日期字符串转换为bean中的Timestamp*/
7 public class TimestampMorpher extends AbstractObjectMorpher {
8 /*** 日期字符串格式*/
9 private String[] formats;
10 public TimestampMorpher(String[] formats) {
11 this.formats = formats;
12 }
13 public Object morph(Object value) {
14 if( value == null){
15 return null;
16 }
17 if( Timestamp.class.isAssignableFrom(value.getClass()) ){
18 return (Timestamp) value;
19 }
20 if( !supports( value.getClass()) ){
21 throw new MorphException( value.getClass() + " 是不支持的类型");
22 }
23 String strValue=(String) value;
24 SimpleDateFormat dateParser=null;
25 for( int i = 0; i < formats.length ; i++ ){
26 if( null == dateParser ){
27 dateParser=new SimpleDateFormat(formats[i]);
28 }else{
29 dateParser.applyPattern(formats[i]);
30 }
31 try{
32 return new Timestamp( dateParser.parse( strValue.toLowerCase()).getTime() );}
33 catch (ParseException e) {
34 //e.printStackTrace();
35 }
36 }
37 return null;
38 }
39 @Override
40 public Class morphsTo() {
41 return Timestamp.class;
42 }
43 public boolean supports( Class clazz ){
44 return String.class.isAssignableFrom( clazz );
45 }
46
47 }
测试一下。
http://www.etf88.com/djxw/srys/2015/0214/206758.shtml
http://www.etf88.com/djxw/srys/2015/0214/206757.shtml
http://www.etf88.com/djxw/srys/2015/0214/206756.shtml
http://www.etf88.com/djxw/srys/2015/0214/206755.shtml
http://www.etf88.com/djxw/srys/2015/0214/206754.shtml
http://www.etf88.com/djxw/srys/2015/0214/206752.shtml
http://www.etf88.com/djxw/srys/2015/0214/206750.shtml
http://www.etf88.com/djxw/srys/2015/0214/206748.shtml
http://www.etf88.com/djxw/srys/2015/0214/206746.shtml
http://www.etf88.com/djxw/srys/2015/0214/206744.shtml
http://www.etf88.com/djxw/srys/2015/0214/206742.shtml
http://www.etf88.com/djxw/srys/2015/0214/206740.shtml
http://www.etf88.com/djxw/srys/2015/0214/206739.shtml
http://www.etf88.com/djxw/srys/2015/0214/206738.shtml
http://www.whtv.com.cn/bjyp/scyy/2015021414057.html
http://www.whtv.com.cn/bjyp/scyy/2015021414056.html
http://www.whtv.com.cn/bjyp/scyy/2015021414055.html
http://www.whtv.com.cn/bjyp/scyy/2015021414054.html
http://www.whtv.com.cn/bjyp/scyy/2015021414053.html
http://www.whtv.com.cn/bjyp/scyy/2015021414052.html
http://www.whtv.com.cn/bjyp/scyy/2015021414051.html
http://www.whtv.com.cn/bjyp/scyy/2015021414050.html
http://www.whtv.com.cn/bjyp/scyy/2015021414049.html
http://www.whtv.com.cn/bjyp/scyy/2015021414048.html
http://www.whtv.com.cn/bjyp/scyy/2015021414047.html
http://www.whtv.com.cn/bjyp/scyy/2015021414046.html
http://www.whtv.com.cn/bjyp/scyy/2015021414045.html
http://www.whtv.com.cn/bjyp/scyy/2015021414044.html
http://www.whtv.com.cn/bjyp/scyy/2015021414043.html
http://www.whtv.com.cn/bjyp/scyy/2015021414042.html
http://www.whtv.com.cn/bjyp/scyy/2015021414041.html
http://www.whtv.com.cn/bjyp/scyy/2015021414040.html
http://www.whtv.com.cn/bjyp/scyy/2015021414039.html
http://www.whtv.com.cn/bjyp/scyy/2015021414038.html
http://www.whtv.com.cn/bjyp/scyy/2015021414037.html
http://www.whtv.com.cn/bjyp/scyy/2015021414036.html
http://www.whtv.com.cn/bjyp/scyy/2015021414035.html
http://www.whtv.com.cn/bjyp/scyy/2015021414034.html
http://www.whtv.com.cn/bjyp/scyy/2015021414033.html
http://www.whtv.com.cn/bjyp/scyy/2015021414032.html
http://www.whtv.com.cn/bjyp/scyy/2015021414031.html
http://www.whtv.com.cn/bjyp/scyy/2015021414030.html
http://www.whtv.com.cn/bjyp/scyy/2015021414029.html
http://www.whtv.com.cn/bjyp/scyy/2015021414028.html
http://www.whtv.com.cn/bjyp/scyy/2015021414027.html
http://www.whtv.com.cn/bjyp/scyy/2015021414026.html
http://www.whtv.com.cn/bjyp/scyy/2015021414025.html
http://www.whtv.com.cn/bjyp/scyy/2015021414024.html
http://www.whtv.com.cn/bjyp/scyy/2015021414023.html
http://www.whtv.com.cn/bjyp/scyy/2015021414022.html
http://www.whtv.com.cn/bjyp/scyy/2015021414021.html
http://www.zhnews.net/kpzx/bhwd/8013719066.html
http://www.zhnews.net/kpzx/bhwd/8013714847.html
http://www.zhnews.net/kpzx/bhwd/8013714675.html
http://www.zhnews.net/kpzx/bhwd/8013714291.html
http://www.zhnews.net/kpzx/bhwd/8013714391.html
http://www.zhnews.net/kpzx/bhwd/8013714055.html
http://www.zhnews.net/kpzx/bhwd/8013714711.html
http://www.zhnews.net/kpzx/bhwd/8013719102.html
http://www.zhnews.net/kpzx/bhwd/8013718855.html
http://www.zhnews.net/kpzx/bhwd/8013718443.html
http://www.zhnews.net/kpzx/bhwd/8013718307.html
http://www.zhnews.net/kpzx/bhwd/8013718168.html
http://www.zhnews.net/kpzx/bhwd/8013721346.html
http://www.zhnews.net/kpzx/bhwd/8013719202.html
http://www.51etong.com/kpzx/jkzn/7687505400.html
http://www.51etong.com/kpzx/jkzn/7687505384.html
http://www.51etong.com/kpzx/jkzn/7687505387.html
http://www.51etong.com/kpzx/jkzn/7687505385.html
http://www.51etong.com/kpzx/jkzn/7687505386.html
http://www.51etong.com/kpzx/jkzn/7687505383.html
http://www.51etong.com/kpzx/jkzn/7687505382.html
http://www.51etong.com/kpzx/jkzn/7687505394.html
http://www.51etong.com/kpzx/jkzn/7687505393.html
http://www.51etong.com/kpzx/jkzn/7687505392.html
http://www.51etong.com/kpzx/jkzn/7687505390.html
http://www.51etong.com/kpzx/jkzn/7687505391.html
http://www.51etong.com/kpzx/jkzn/7687505389.html
http://www.51etong.com/kpzx/jkzn/7687505388.html
http://www.51etong.com/kpzx/jkzn/7687505399.html
http://www.51etong.com/kpzx/jkzn/7687505398.html
http://www.51etong.com/kpzx/jkzn/7687505397.html
http://www.51etong.com/kpzx/jkzn/7687505396.html
http://www.51etong.com/kpzx/jkzn/7687505395.html
http://www.lzbs.com.cn/yszx/sjst/8012949666.html
http://www.lzbs.com.cn/yszx/sjst/8012949347.html
http://www.lzbs.com.cn/yszx/sjst/8012949530.html
http://www.lzbs.com.cn/yszx/sjst/8012950122.html
http://www.lzbs.com.cn/yszx/sjst/8012953555.html
http://www.lzbs.com.cn/yszx/sjst/8012948368.html
http://www.lzbs.com.cn/yszx/sjst/8012948196.html
http://www.lzbs.com.cn/yszx/sjst/8012947922.html
http://www.lzbs.com.cn/yszx/sjst/8012947566.html
http://www.lzbs.com.cn/yszx/sjst/8012947366.html
http://www.lzbs.com.cn/yszx/sjst/8012947466.html
http://www.lzbs.com.cn/yszx/sjst/8012947111.html
http://www.lzbs.com.cn/yszx/sjst/8012950551.html
http://www.lzbs.com.cn/yszx/sjst/8012950341.html
http://www.lzbs.com.cn/yszx/sjst/8012949766.html
http://www.020.com/ys/sjys/8013834846.html
http://www.020.com/ys/sjys/8013834098.html
http://www.020.com/ys/sjys/8013833762.html
http://www.020.com/ys/sjys/8013836327.html
http://www.020.com/ys/sjys/8013836462.html
http://www.020.com/ys/sjys/8013833527.html
http://www.020.com/ys/sjys/8013836896.html
http://www.020.com/ys/sjys/8013833662.html
http://www.020.com/ys/sjys/8013832396.html
http://www.020.com/ys/sjys/8013835296.html
http://www.020.com/ys/sjys/8013832138.html
http://www.020.com/ys/sjys/8013832496.html
http://www.020.com/ys/sjys/8013831966.html
http://www.020.com/ys/sjys/8013834982.html
标签:
原文地址:http://www.cnblogs.com/dsgr1321/p/4291349.html