json-lib序列化java.sql.Date

 

import  java.util.HashMap;

import  net.sf.json.JSONObject;
import  net.sf.json.JsonConfig;
import  net.sf.json.processors.JsDateJsonBeanProcessor;

public   class  JsonDate {

    
public   static   void  main(String[] args) {
        
//  json-lib默认不支持java.sql.Date的序列化,要序列化自己的类,实现一个BeanProcessor处理即可
        JsDateJsonBeanProcessor beanProcessor  =   new  JsDateJsonBeanProcessor();
        java.sql.Date d 
=   new  java.sql.Date(System.currentTimeMillis());

        
//  直接序列化
        JsonConfig config  =   new  JsonConfig();
        JSONObject json 
=  beanProcessor.processBean(d, config);
        System.out.println(json.toString());

        
//  序列化含java.sql.Date作为属性值的bean
        HashMap m  =   new  HashMap();
        m.put(
" date " , d);
        config.registerJsonBeanProcessor(java.sql.Date.
class , beanProcessor);
        json 
=  JSONObject.fromObject(m, config);
        System.out.println(json.toString());
    }
}
{"year":2010,"month":11,"day":18,"hours":22,"minutes":31,"seconds":47,"milliseconds":572}
{"date":{"year":2010,"month":11,"day":18,"hours":22,"minutes":31,"seconds":47,"milliseconds":572}}

你可能感兴趣的:(java,sql,bean,json,.net)