J2EE社區(qū)

          茍有恒,何必三更起五更眠;
          最無益,只怕一日曝十日寒.
          posts - 241, comments - 318, trackbacks - 0, articles - 16

          JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換

          Posted on 2008-10-31 02:47 xcp 閱讀(117586) 評(píng)論(11)  編輯  收藏
          關(guān)鍵字: json java
          JSON-lib這個(gè)Java類包用于把bean,map和XML轉(zhuǎn)換成JSON并能夠把JSON轉(zhuǎn)回成bean和DynaBean。

          下載地址:http://json-lib.sourceforge.net/
          還要需要的第3方包:
          org.apache.commons(3.2以上版本)
          org.apache.oro
          net.sf.ezmorph(ezmorph-1.0.4.jar)
          nu.xom

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

          3、BEAN
          Java代碼
          1. /**
          2.       * Bean.java
          3.          private String name = "json";   
          4.          private int pojoId = 1;   
          5.          private char[] options = new char[]{'a','f'};   
          6.          private String func1 = "function(i){ return this.options[i]; }";   
          7.          private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
          8.      */   
          9. JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );      
          10. System.out.println( jsonObject );      
          11. //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}    

          4、BEANS
          Java代碼
          1. /**
          2.        * private int row ;
          3.            private int col ;
          4.            private String value ;
          5.        *
          6.        */  
          7. List list = new ArrayList();   
          8.           JsonBean2 jb1 = new JsonBean2();   
          9.           jb1.setCol(1);   
          10.           jb1.setRow(1);   
          11.           jb1.setValue("xx");   
          12.             
          13.           JsonBean2 jb2 = new JsonBean2();   
          14.           jb2.setCol(2);   
          15.           jb2.setRow(2);   
          16.           jb2.setValue("");   
          17.             
          18.             
          19.           list.add(jb1);   
          20.           list.add(jb2);   
          21.             
          22.           JSONArray ja = JSONArray.fromObject(list);   
          23.           System.out.println( ja.toString() );   
          24.          //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]  

          5、String to bean
          Java代碼
          1. String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";      
          2. JSONObject jsonObject = JSONObject.fromString(json);      
          3. Object bean = JSONObject.toBean( jsonObject );      
          4. assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );      
          5.    assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );      
          6.    assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );      
          7.     assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );      
          8.     assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );      
          9.    List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );      
          10.    assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );    


          Java代碼
          1. String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";      
          2. JSONObject jsonObject = JSONObject.fromString(json);   
          3.    JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );      
          4.     assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol())   );      
          5.       assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );      
          6.       assertEquals( jsonObject.get( "value" ), bean.getValue() );    



          6 json to xml
          1)
          JSONObject json = new JSONObject( true );
          String xml = XMLSerializer.write( json );

          <o class="object" null="true">

          2)
          JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
          String xml = XMLSerializer.write( json );
          <o class="object">
          <name type="string">json</name>
          <bool type="boolean">true</bool>
          <int type="number">1</int>
          </o>
          <o class="object">
          <name type="string">json</name>
          <bool type="boolean">true</bool>
          <int type="number">1</int>
          </o>
          3)
          JSONArray json = JSONArray.fromObject("[1,2,3]");
          String xml = XMLSerializer.write( json );
          <a class="array">
          <e type="number">1</e>
          <e type="number">2</e>
          <e type="number">3</e>
          </a>

          7 、xml to json
          <a class="array">
          <e type="function" params="i,j">
          return matrix[i][j];
          </e>
          </a>
          <a class="array">
          <e type="function" params="i,j">
          return matrix[i][j];
          </e>
          </a>

          JSONArray json = (JSONArray) XMLSerializer.read( xml );
          System.out.println( json );
          // prints [function(i,j){ return matrix[i][j]; }]

          出自http://falchion.javaeye.com/




          名稱: ?4C.ESL | .↗Evon
          口號(hào): 遇到新問題?先要尋找一個(gè)方案乄而不是創(chuàng)造一個(gè)方案こ
          mail: 聯(lián)系我


          Feedback

          # re: JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評(píng)論   

          2008-10-31 11:27 by jasin
          官網(wǎng)上的例子

          # re: JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評(píng)論   

          2008-11-04 14:59 by zhyiwww
          不錯(cuò)

          # re: JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評(píng)論   

          2009-06-01 10:49 by hnnh86@gmail.com
          官網(wǎng)上這個(gè)例子我在MyEclipse7.1下跑不動(dòng)啊。
          package jsontest;

          import junit.framework.TestCase;
          import net.sf.json.JSONArray;

          public class JsonLibTest extends TestCase {

          public void testJsonArray() {
          boolean[] boolArray = new boolean[]{true,false,true};
          JSONArray jsonArray = JSONArray.fromObject( boolArray );
          System.out.println( jsonArray );
          }
          }

          # re: JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評(píng)論   

          2009-06-22 15:15 by 冰河快狼
          不錯(cuò)的了,支持

          # re: JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換[未登錄]  回復(fù)  更多評(píng)論   

          2011-06-16 15:23 by 笑笑
          /**
          */
          public static void main(String[] orgs){

          }

          # re: JSON與JAVA數(shù)據(jù)轉(zhuǎn)換  回復(fù)  更多評(píng)論   

          2011-11-15 17:20 by ..
          ..

          # re: JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評(píng)論   

          2011-12-28 08:24 by JSON在線校驗(yàn)、視圖、格式化工具
          可以使用www.bejson.com這個(gè)JSON在線工具做JSON的格式校驗(yàn)和數(shù)據(jù)視圖

          # re: JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換[未登錄]  回復(fù)  更多評(píng)論   

          2012-06-26 17:03 by 李強(qiáng)
          最近我發(fā)現(xiàn)有的時(shí)候添加設(shè)置JSON對(duì)象的屬性的時(shí)候,如果屬性本身也是JSON對(duì)象一定要先設(shè)置屬性的JSON對(duì)象的特性,再加到父JSON對(duì)象中,才能使得父JSON對(duì)象轉(zhuǎn)換成字符串中體現(xiàn)其屬性JSON對(duì)象特性,,我猜測(cè)可能加入的屬性是直接轉(zhuǎn)換成字符串了,即信息而不是屬性JSON對(duì)象的引用.

          # re: JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評(píng)論   

          2013-11-18 14:47 by 5555
          22222

          # re: JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評(píng)論   

          2013-12-18 21:09 by JSON.com

          推薦一個(gè)JSON格式化工具,JSON校驗(yàn)工具, http://www.sojson.com
          無廣告加載快的工具

          # re: JSON與JAVA數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評(píng)論   

          2014-05-23 10:13 by zuidaima
          json代碼下載:http://www.zuidaima.com/share/search.htm?key=json

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 韶关市| 东宁县| 济源市| 江达县| 高雄市| 精河县| 天长市| 张掖市| 建始县| 特克斯县| 新郑市| 民县| 六盘水市| 青岛市| 南安市| 临湘市| 莱阳市| 古蔺县| 石首市| 新乡市| 芜湖市| 五常市| 琼中| 绥阳县| 常宁市| 瑞金市| 新源县| 广南县| 泉州市| 滦南县| 开平市| 孙吴县| 江北区| 尼勒克县| 潢川县| 黑水县| 葵青区| 桐城市| 许昌县| 从化市| 皋兰县|