Json與Bean互轉,Timestamp型別的問題
阿新 • • 發佈:2019-02-05
Json與Java Bean互相轉換時,Bean中的Timestamp欄位是無法直接處理的,需要實現兩個轉換器。
DateJsonValueProcessor的作用是Bean轉換為Json時將Timepstamp轉換為指定的時間格式。
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; /*** 將Bean中的Timestamp轉換為json中的日期字串*/ public class DateJsonValueProcessor implements JsonValueProcessor { public static final String Default_DATE_PATTERN ="yyyy-MM-dd"; private DateFormat dateFormat ; public DateJsonValueProcessor(String datePattern){ try{ dateFormat = new SimpleDateFormat(datePattern);} catch(Exception e ){ dateFormat = new SimpleDateFormat(Default_DATE_PATTERN); } } public Object processArrayValue(Object value, JsonConfig jsonConfig) { return process(value); } public Object processObjectValue(String key, Object value,JsonConfig jsonConfig) { return process(value); } private Object process(Object value){ return dateFormat.format((Date)value); } }
TimestampMorpher的作用則與DateJsonValueProcessor相反,它是在Jsonl轉換為Bean時,會把指定的時間格式轉換為Timestamp
import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.object.AbstractObjectMorpher; /*** 將json串中的日期字串轉換為bean中的Timestamp*/ public class TimestampMorpher extends AbstractObjectMorpher { /*** 日期字串格式*/ private String[] formats; public TimestampMorpher(String[] formats) { this.formats = formats; } public Object morph(Object value) { if( value == null){ return null; } if( Timestamp.class.isAssignableFrom(value.getClass()) ){ return (Timestamp) value; } if( !supports( value.getClass()) ){ throw new MorphException( value.getClass() + " 是不支援的型別"); } String strValue=(String) value; SimpleDateFormat dateParser=null; for( int i = 0; i < formats.length ; i++ ){ if( null == dateParser ){ dateParser=new SimpleDateFormat(formats[i]); }else{ dateParser.applyPattern(formats[i]); } try{ return new Timestamp( dateParser.parse( strValue.toLowerCase()).getTime() );} catch (ParseException e) { //e.printStackTrace(); } } return null; } @Override public Class morphsTo() { return Timestamp.class; } public boolean supports( Class clazz ){ return String.class.isAssignableFrom( clazz ); } } public class JsonTest { public static void main(String[] args) { String jsonStr="{\"id\":\"101\",\"name\":\"張三\",\"age\":\"20\",\"birthday\":\"1992-10-19 23:52:18\"}"; Student s=new Student(); Timestamp b=Timestamp.valueOf("1992-10-19 23:52:18"); s.setId(123456); s.setName("李四"); s.setAge(20); s.setBirthday(b); Student s1=jsonToBean(jsonStr); System.out.println(s1.getBirthday()); System.out.println(beanToJson(s)); } public static Student jsonToBean(String json){ String[] formats={"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"}; JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher(formats)); JSONObject jsonObject=JSONObject.fromObject(json); return (Student)JSONObject.toBean(jsonObject,Student.class); } public static String beanToJson(Student s){ JsonConfig config=new JsonConfig(); config.registerJsonValueProcessor(Timestamp.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss")); JSONObject json=JSONObject.fromObject(s,config); return json.toString();} }