fastjson对Date的处理

对日期的序列化:(http://yidao620c.iteye.com/blog/2036853

一种方法是通过注解

Java代码   收藏代码
  1. @JSONField (format="yyyy-MM-dd HH:mm:ss")  
  2. public Date birthday;  

另一种是通过SerializeConfig:

Java代码   收藏代码
  1. private static SerializeConfig mapping = new SerializeConfig();  
  2. private static String dateFormat;  
  3. static {  
  4.     dateFormat = "yyyy-MM-dd HH:mm:ss";  
  5.     mapping.put(Date.classnew SimpleDateFormatSerializer(dateFormat));  
  6. }  

 

json字符串中使用单引号:

String text = JSON.toJSONString(object, SerializerFeature.UseSingleQuotes);

 

字段显示不同的key:

 

Java代码   收藏代码
  1. public class User {  
  2.     @JSONField(name="ID")  
  3.     public int getId() { ... }  
  4. }  
  5.    
  6. User user = ...;  
  7. JSON.toJSONString(user); // {"ID":001}  

 

类的反序列化 JavaBean:

 

Java代码   收藏代码
  1. String text = ...; // {"r":255,"g":0,"b":0,"alpha":255}  
  2. Color color = JSON.parseObject(text, Color.class);  

 

数组:

 

Java代码   收藏代码
  1. String text = ...; // [{ ... }, { ... }]  
  2. List<User> users = JSON.parseArray(text, User.class);  

 

泛型:

Java代码   收藏代码
  1. String text = ...; // {"name":{"name":"ljw",age:18}}  
  2. Map<String, User> userMap = JSON.parseObject(text, new TypeReference<Map<String, User>>() {});  

 

自定义序列化代码示例:

 

Java代码   收藏代码
  1. public class JsonUtil {  
  2.     private static SerializeConfig mapping = new SerializeConfig();  
  3.     private static String dateFormat;  
  4.     static {  
  5.         dateFormat = "yyyy-MM-dd HH:mm:ss";  
  6.     }  
  7.   
  8.     /** 
  9.      * 默认的处理时间 
  10.      *  
  11.      * @param jsonText 
  12.      * @return 
  13.      */  
  14.     public static String toJSON(Object jsonText) {  
  15.         return JSON.toJSONString(jsonText,  
  16.                 SerializerFeature.WriteDateUseDateFormat);  
  17.     }  
  18.   
  19.     /** 
  20.      * 自定义时间格式 
  21.      *  
  22.      * @param jsonText 
  23.      * @return 
  24.      */  
  25.     public static String toJSON(String dateFormat, String jsonText) {  
  26.         mapping.put(Date.classnew SimpleDateFormatSerializer(dateFormat));  
  27.         return JSON.toJSONString(jsonText, mapping);  
  28.     }  
  29. }  

 

自定义反序列化示例:

先自定义一个日期解析类:

Java代码   收藏代码
  1. public class MyDateFormatDeserializer extends DateFormatDeserializer {  
  2.   
  3.         private String myFormat;  
  4.   
  5.         public MyDateFormatDeserializer(String myFormat) {  
  6.             super();  
  7.             this.myFormat = myFormat;  
  8.         }  
  9.   
  10.         @Override  
  11.         protected <Date> Date cast(DefaultJSONParser parser, Type clazz, Object fieldName, Object val) {  
  12.             if (myFormat == null) {  
  13.                 return null;  
  14.             }  
  15.             if (val instanceof String) {  
  16.                 String strVal = (String) val;  
  17.                 if (strVal.length() == 0) {  
  18.                     return null;  
  19.                 }  
  20.   
  21.                 try {  
  22.                     return (Date) new SimpleDateFormat(myFormat).parse((String)val);  
  23.                 } catch (ParseException e) {  
  24.                     throw new JSONException("parse error");  
  25.                 }  
  26.             }  
  27.             throw new JSONException("parse error");  
  28.         }  
  29.     }  

 

Java代码   收藏代码
  1. public class User {  
  2.   
  3.     public String name;  
  4.     public int height;  
  5.   
  6.     @JSONField(name = "com-google-com")  
  7.     public void setName(String name) {  
  8.         this.name = name;  
  9.     }  
  10.   
  11.     @JSONField(format = "yyyy-MM/dd HH:mm:ss")  
  12.     public Date birthday;  
  13. }  

 

测试下:

Java代码   收藏代码
  1. /** 
  2.  * @param args 
  3.  * @throws IOException 
  4.  */  
  5. public static void main(String[] args) throws IOException, ParseException {  
  6.   
  7.     String json = "{\"name\":\"22323\", \"age\": 1234," +  
  8.             " \"birthday\": \"2012-12/12 12:12:12\"}";  
  9.     Test t = JSON.parseObject(json, Test.class, mapping,  
  10.             JSON.DEFAULT_PARSER_FEATURE, new Feature[0]);  
  11.     System.out.println(t.name);  
  12.     System.out.println(t.height);  
  13.     System.out.println(t.birthday);  
  14.     System.out.println(  
  15.             new SimpleDateFormat("yyyy-MM/dd HH:mm:ss").parse("2012-12/12 12:12:12"));  
  16. }  

 

总结:对于JSONField注解,好像只对序列号的格式有影响,反序列化不管这个,不知道为什么,只能自己写个解析类了,不过这样就更灵活了,可以在里面写很多处理逻辑,比如json字符串里面日期格式并不是标准格式的时候,就可以先转成标准格式再去解析了。

 

另外,fastjson的document官网一直打不开,不知道神马原因,难道被feng了麽:

http://code.alibabatech.com/wiki/display/FastJSON/Documentation

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值