傲慢和骄傲的区别:使用JSON-LIB转换JAVA对象

来源:百度文库 编辑:偶看新闻 时间:2024/05/06 01:06:21

使用JSON-LIB转换JAVA对象

    博客分类:
  • jQuery
jsonJava.netAjaxTomcat

使用JSON-LIB可以极大的简化JAVA对象转换成JSON对象所需进行的操作,更可以避免人工操作生成JSON对象字符串时带来的麻烦和误操作:
使用JSON-LIB,首先要有几个支持的包:
http://json-lib.sourceforge.net下载json-lib-1.1-jdk15.jar
commons-lang.jar、commons-logging.jar,commons-beanutils.jar  这些包可在tomcat/comon/lib下找到
EZMorph 下载地址http://ezmorph.sourceforge.net
morph-1.0.1 下载地址:http://morph.sourceforge.net

使用例子:

Java代码  
  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import net.sf.json.JSONArray;  
  7. import net.sf.json.JSONObject;  
  8.   
  9. public class JSONTest {  
  10.     public static void main(String[] args) {     
  11.         JSONTest j = new JSONTest();     
  12.         j.ObjectList2json();     
  13.     }      
  14.      
  15.     public void ObjectList2json(){  
  16.         Map map = new HashMap();  
  17.           
  18.         List jlist = new ArrayList();  
  19.         JSONTestBean bean1 = new JSONTestBean("zhangbo","123123");  
  20.         JSONTestBean bean2 = new JSONTestBean("lisi","65489");  
  21.         Props props = new Props("127.0.0.1","8008");  
  22.           
  23.         jlist.add(bean1);  
  24.         jlist.add(bean2);  
  25.           
  26.         map.put("Props", props);  
  27.         map.put("jsonObjectList", jlist);  
  28.           
  29.         JSONArray jsonArray = JSONArray.fromObject(map);     
  30.         System.out.println(jsonArray);     
  31.     }  
  32.       
  33.     public void arr2json() {     
  34.         boolean[] boolArray = new boolean[] { true, false, true };     
  35.         JSONArray jsonArray = JSONArray.fromObject(boolArray);     
  36.         System.out.println(jsonArray);     
  37.         // prints [true,false,true]     
  38.     }     
  39.      
  40.     public void list2json() {     
  41.         List list = new ArrayList();    
  42.         list.add("first");     
  43.         list.add("second");     
  44.         JSONArray jsonArray = JSONArray.fromObject(list);     
  45.         System.out.println(jsonArray);     
  46.         // prints ["first","second"]     
  47.     }     
  48.      
  49.     public void createJson() {     
  50.         JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");     
  51.         System.out.println(jsonArray);     
  52.         // prints ["json","is","easy"]     
  53.     }     
  54.      
  55.     public void map2json() {     
  56.         Map map = new HashMap();  
  57.         map.put("name", "json");     
  58.         map.put("bool", Boolean.TRUE);     
  59.         map.put("int", new Integer(1));     
  60.         map.put("arr", new String[] { "a", "b" });     
  61.         map.put("func", "function(i){ return this.arr[i]; }");     
  62.      
  63.         JSONObject json = JSONObject.fromObject(map);     
  64.         System.out.println(json);     
  65.         // prints     
  66.         // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){     
  67.         // return this.arr[i]; }]     
  68.     }     
  69.      
  70.     public void bean2json() {     
  71.         JSONObject jsonObject = JSONObject.fromObject(new JSONTestBean("zhangbo","234234"));     
  72.         System.out.println(jsonObject);     
  73.         /*   
  74.          * prints    
  75.          * {"func1":function(i){ return this.options[i];   
  76.          * },"pojoId":1,"name":"json","func2":function(i){ return   
  77.          * this.options[i]; }}   
  78.          */     
  79.     }     
  80.      
  81.     public void json2bean() {     
  82.         String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";     
  83. //        JSONObject jb = JSONObject.fromString(json);     
  84. //        JSONObject.toBean(jb, MyBean.class);     
  85.         System.out.println();     
  86.     }     
  87. }  
  88.   
  89. 其它两个测试实体Bean:  
  90. public class JSONTestBean {  
  91.   
  92.     private String userName;  
  93.   
  94.     private String password;  
  95.   
  96.     public JSONTestBean() {  
  97.   
  98.     }  
  99.   
  100.     public JSONTestBean(String username, String password) {  
  101.         this.userName = username;  
  102.         this.password = password;  
  103.     }  
  104.   
  105.     public String getPassword() {  
  106.         return password;  
  107.     }  
  108.   
  109.     public void setPassword(String password) {  
  110.         this.password = password;  
  111.     }  
  112.   
  113.     public String getUserName() {  
  114.         return userName;  
  115.     }  
  116.   
  117.     public void setUserName(String userName) {  
  118.         this.userName = userName;  
  119.     }  
  120. }  
  121.   
  122. //===================================================  
  123. public class Props {  
  124.     private String ip;  
  125.     private String port;  
  126.   
  127.     public Props() {  
  128.     }  
  129.   
  130.     public Props(String ip, String port) {  
  131.         this.ip = ip;  
  132.         this.port = port;  
  133.     }  
  134.   
  135.     public String getIp() {  
  136.         return ip;  
  137.     }  
  138.   
  139.     public void setIp(String ip) {  
  140.         this.ip = ip;  
  141.     }  
  142.   
  143.     public String getPort() {  
  144.         return port;  
  145.     }  
  146.   
  147.     public void setPort(String port) {  
  148.         this.port = port;  
  149.     }  
  150.   
  151. }  

 

使用起来很方便,有了JSON-LIB的支持,可以使开发者轻松构建起基于JSON的AJAX应用程序

附加:关于使用JSON-LIB转换带有DATE类型的对象需要额外的一些设置

Java代码  
  1. JsonConfig cfg=new JsonConfig();  
  2. cfg.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());  
  3. cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessorImpl());  
  4. JSONObject obj = JSONObject.fromObject(info,cfg);  

 

JsonValueProcessorImpl为实现了源代码中的接口JsonValueProcessor

Java代码  
  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3.   
  4. import net.sf.json.JsonConfig;  
  5. import net.sf.json.processors.JsonValueProcessor;  
  6.   
  7. public class JsonValueProcessorImpl implements JsonValueProcessor{  
  8.  private String format="yyyy-MM-dd";  
  9.  public JsonValueProcessorImpl(){  
  10.     
  11.  }  
  12.  public JsonValueProcessorImpl(String format){  
  13.   this.format=format;  
  14.  }  
  15.  public Object processArrayValue(Object value, JsonConfig jsonConfig) {  
  16.   String[] obj={};  
  17.   if(value instanceof Date[]){  
  18.    SimpleDateFormat sf=new SimpleDateFormat(format);  
  19.    Date[] dates=(Date[])value;  
  20.    obj =new String[dates.length];  
  21.    for (int i = 0; i < dates.length; i++) {  
  22.     obj[i]=sf.format(dates[i]);  
  23.    }  
  24.   }  
  25.   return obj;  
  26.  }  
  27.   
  28.  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {  
  29.   if(value instanceof Date){  
  30.    String str=new SimpleDateFormat(format).format((Date)value);  
  31.    return str;  
  32.   }  
  33.   return value.toString();  
  34.  }  
  35.   
  36.  public String getFormat() {  
  37.   return format;  
  38.  }  
  39.   
  40.  public void setFormat(String format) {  
  41.   this.format = format;  
  42.  }  
  43.    
  44. }   

  

这也只是实现了将DATE类型转换成yyyy-MM-dd的格式...测试一下吧!~~~