JSON与JAVA的数据转换

JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成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

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"]       

16.  

17.  boolean[] boolArray = new boolean[]{true,false,true};     

18.  JSONArray jsonArray1 = JSONArray.fromObject( boolArray );     

19.  System.out.println( jsonArray1 );     

20.   // prints [true,false,true]    

21.     

22.   List list = new ArrayList();     

23.   list.add( "first" );     

24.   list.add( "second" );     

25.   JSONArray jsonArray2 = JSONArray.fromObject( list );     

26.   System.out.println( jsonArray2 );     

27.   // prints ["first","second"]    

28.  

29.   JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );     

30.   System.out.println( jsonArray3 );     

31.   // prints ["json","is","easy"]   
  


2、Map



32.  Map map = new HashMap();       

33. map.put( "name", "json" );       

34. map.put( "bool", Boolean.TRUE );       

35.     

36. map.put( "int", new Integer(1) );       

37. map.put( "arr", new String[]{"a","b"} );       

38. map.put( "func", "function(i){ return this.arr[i]; }" );       

39. JSONObject json = JSONObject.fromObject( map );       

40. System.out.println( json );       

41.  //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}    

42.  

43. Map map = new HashMap();     

44.  map.put( "name", "json" );     

45.  map.put( "bool", Boolean.TRUE );     

46.    

47.  map.put( "int", new Integer(1) );     

48.  map.put( "arr", new String[]{"a","b"} );     

49.  map.put( "func", "function(i){ return this.arr[i]; }" );     

50.  JSONObject json = JSONObject.fromObject( map );     

51.  System.out.println( json );     

52.  //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}   



3、BEAN

53.        

54.    JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );       

55.    System.out.println( jsonObject );       

56.    //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}      

57.  

58.  

59. JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );     

60. System.out.println( jsonObject );     

61.  //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}     

62. 


4、BEANS

63.      

64. List list = new ArrayList();    

65.         JsonBean2 jb1 = new JsonBean2();    

66.         jb1.setCol(1);    

67.         jb1.setRow(1);    

68.         jb1.setValue("xx");    

69.             

70.         JsonBean2 jb2 = new JsonBean2();    

71.         jb2.setCol(2);    

72.         jb2.setRow(2);    

73.         jb2.setValue("");    

74.              

75.         list.add(jb1);    

76.         list.add(jb2);    

77.             

78.         JSONArray ja = JSONArray.fromObject(list);    

79.         System.out.println( ja.toString() );    

80.         //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]   


81. 

82.    

83. List list = new ArrayList();  

84.         JsonBean2 jb1 = new JsonBean2();  

85.         jb1.setCol(1);  

86.         jb1.setRow(1);  

87.         jb1.setValue("xx");  

88.           

89.         JsonBean2 jb2 = new JsonBean2();  

90.         jb2.setCol(2);  

91.         jb2.setRow(2);  

92.         jb2.setValue("");  

93.           

94.           

95.         list.add(jb1);  

96.         list.add(jb2);  

97.           

98.         JSONArray ja = JSONArray.fromObject(list);  

99.         System.out.println( ja.toString() );  

100.          //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]  



5、String to bean

101.      String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";       

102.     JSONObject jsonObject = JSONObject.fromString(json);       

103.     Object bean = JSONObject.toBean( jsonObject );       

104.       assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );       

105.       assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );       

106.       assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );       

107.       assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );       

108.       assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );       

109.       List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );       

110.       assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );      

111.   

112.  String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";     

113.  JSONObject jsonObject = JSONObject.fromString(json);     

114.  Object bean = JSONObject.toBean( jsonObject );     

115.    assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );     

116.    assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );     

117.    assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );     

118.    assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );     

119.    assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );     

120.    List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );     

121.    assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );     

122.   

123.    String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";       

124.    JSONObject jsonObject = JSONObject.fromString(json);    

125.    JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );       

126.        assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol())  );       

127.        assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );       

128.        assertEquals( jsonObject.get( "value" ), bean.getValue() );      

129.   

130.  String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";     

131.  JSONObject jsonObject = JSONObject.fromString(json);  

132.  JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );     

133.      assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol())  );     

134.      assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );     

135.      assertEquals( jsonObject.get( "value" ), bean.getValue() );    


6  json to xml
1)

136.  JSONObject json = new JSONObject( true );  

137.  String xml = XMLSerializer.write( json ); 

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

2)

139.  JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");  

140.  String xml = XMLSerializer.write( json ); 

 

141.  < o class="object"> 

142.     < name type="string">json< /name> 

143.     < bool type="boolean">true< /bool> 

144.     < int type="number">1< /int> 

145.  < /o> 

146.     < o class="object"> 

147.        < name type="string">json< /name> 

148.        < bool type="boolean">true< /bool> 

149.        < int type="number">1< /int> 

150.     < /o> 

3)

151.  JSONArray json = JSONArray.fromObject("[1,2,3]");  

152.  String xml = XMLSerializer.write( json ); 

 

153.  < a class="array"> 

154.     < e type="number">1< /e> 

155.     < e type="number">2< /e> 

156.     < e type="number">3< /e> 

157.  < /a> 

7 、xml to json

158.  < a class="array"> 

159.    < e type="function" params="i,j"> 

160.        return matrix[i][j];  

161.    < /e> 

162.  < /a> 

163.     < a class="array"> 

164.       < e type="function" params="i,j"> 

165.           return matrix[i][j];  

166.       < /e> 

167.     < /a> 

168.  JSONArray json = (JSONArray) XMLSerializer.read( xml );  

169.  System.out.println( json );  

170.  // prints [function(i,j){ return matrix[i][j]; }]   




引自: http://developer.51cto.com/art/200906/129090.htm

你可能感兴趣的:(java,bean,json,xml,J#)