jialisoftw

JSON与JAVA的数据转换

本文介绍如何使用JSON-lib这个Java类包实现Java的数据转换。通过此类包可以把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。

JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。

下载地址:

http://sourceforge.net/projects/json-lib/

还要需要的第3方包:
org.apache.commons

包括:

commons-beanutils.jar
commons-collections-3.2.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar

net.sf.ezmorph(ezmorph-1.0.6.jar)

 

1. 对List类型的解析:

Java代码:  
  1. boolean[] boolArray = new boolean[]{true,false,true};          
  2.         JSONArray jsonArray1 = JSONArray.fromObject( boolArray );         
  3.         System.out.println( jsonArray1 );         
  4.          // prints [true,false,true]        
  5.               
  6.          List list = new ArrayList();         
  7.          list.add( "first" );         
  8.          list.add( "second" );         
  9.          JSONArray jsonArray2 = JSONArray.fromObject( list );         
  10.          System.out.println( jsonArray2 );         
  11.          // prints ["first","second"]     
  12.            
  13.          JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );          
  14.          System.out.println( jsonArray3 );          
  15.          // prints ["json","is","easy"] 

2. 对Map类型的解析

 原文参考自站长网:http://www.software8.co/wzjs/java/3165.html

Java代码:  
  1. Map map = new HashMap();  
  2. map.put("name", "json");  
  3. map.put("bool", Boolean.TRUE);  
  4. map.put("int", new Integer(1));  
  5. map.put("arr", new String[] { "a", "b" });  
  6. map.put("func", "function(i){ return this.arr[i]; }");  
  7. JSONObject json = JSONObject.fromObject(map);  
  8. System.out.println(json);   
  9. // {"arr":["a","b"],"int":1,"name":"json","func":function(i){ return this.arr[i]; },"bool":true}  

3、对JavaBEAN的解析

Java代码:  
  1. import net.sf.json.*;  
  2.   
  3. public class Test {  
  4.       
  5.     public static class JsonBean{  
  6.         private String name="json";  
  7.         private int pojoId = 1;  
  8.         public String getName() {  
  9.             return name;  
  10.         }  
  11.         public void setName(String name) {  
  12.             this.name = name;  
  13.         }  
  14.         public int getPojoId() {  
  15.             return pojoId;  
  16.         }  
  17.         public void setPojoId(int pojoId) {  
  18.             this.pojoId = pojoId;  
  19.         }   
  20.     }  
  21.     /** 
  22.      * @param args 
  23.      */  
  24.     public static void main(String[] args) {  
  25.         JSONObject jsonObject = JSONObject.fromObject(new JsonBean());  
  26.         System.out.println(jsonObject);   
  27.         // {"name":"json","pojoId":1}  
  28.     }  
  29. }  

 说明apache json-lib可以满足各种json的解析。

posted on 2013-03-04 08:42 飞猪一号 阅读(2804) 评论(4)  编辑  收藏

评论

# re: JSON与JAVA的数据转换 2013-03-04 23:53 呵乐猫

fastjson不更好用么  回复  更多评论   

# re: JSON与JAVA的数据转换 2013-03-05 10:40 Unmi

Gson 和 Jackson 这两个库很棒的。  回复  更多评论   

# re: JSON与JAVA的数据转换 2013-03-05 20:32 厦会计代理司

这个方式还是不错的呵呵  回复  更多评论   

# re: JSON与JAVA的数据转换 2013-03-06 10:34 红色石头

不是有java bean和json直接转换的开源jar包吗?  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2013年3月>
242526272812
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿

随笔档案

友情链接

搜索

最新评论

阅读排行榜

评论排行榜