Java世界

          學習筆記

          常用鏈接

          統計

          積分與排名

          天籟村

          新華網

          雅虎

          最新評論

          JSON與JAVA數據的轉換

          1、List
          Java代碼
          1. boolean[] boolArray = newboolean[]{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]; }]

          posted on 2009-05-21 15:12 Rabbit 閱讀(1498) 評論(3)  編輯  收藏

          評論

          # re: JSON與JAVA數據的轉換 2014-11-28 19:38 7

          7  回復  更多評論   

          # re: JSON與JAVA數據的轉換 2014-11-28 19:39 72

          ghfhfhfhg  回復  更多評論   

          # re: JSON與JAVA數據的轉換 2014-11-28 19:40 小五

          Java代碼
          String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
          JSONObject jsonObject = JSONObject.fromString(json);
          Object bean = JSONObject.toBean( jsonObject );
          assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
          assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
          assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
          assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
          assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
          List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );
          assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
            回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 安国市| 合川市| 蓬溪县| 太仆寺旗| 玛曲县| 聊城市| 沽源县| 阿克陶县| 东阳市| 龙州县| 香格里拉县| 汉寿县| 资中县| 汶川县| 天全县| 丹巴县| 邵阳市| 南召县| 寿光市| 武义县| 泗阳县| 武邑县| 龙陵县| 张家界市| 西乌珠穆沁旗| 湖口县| 德令哈市| 绵阳市| 五寨县| 新化县| 永和县| 威海市| 库车县| 盐城市| 沈丘县| 扎囊县| 礼泉县| 玛纳斯县| 三亚市| 桂阳县| 山阳县|