您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何進行JSON與JAVA的數據轉換,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
JSON-lib這個Java類包用于把bean,map和XML轉換成JSON并能夠把JSON轉回成bean和DynaBean。
還要需要的第3方包:
org.apache.commons(3.2以上版本)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom
1、List
boolean[] boolArray = new boolean[]{true,false,true}; JSONArray jsonArray1 = JSONArray.fromObject( boolArray ); System.out.println( jsonArray1 ); // prints [true,false,true] List list = new ArrayList(); list.add( "first" ); list.add( "second" ); JSONArray jsonArray2 = JSONArray.fromObject( list ); System.out.println( jsonArray2 ); / prints ["first","second"] JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" ); System.out.println( jsonArray3 ); // prints ["json","is","easy"] boolean[] boolArray = new boolean[]{true,false,true}; JSONArray jsonArray1 = JSONArray.fromObject( boolArray ); System.out.println( jsonArray1 ); // prints [true,false,true] List list = new ArrayList(); list.add( "first" ); list.add( "second" ); JSONArray jsonArray2 = JSONArray.fromObject( list ); System.out.println( jsonArray2 ); // prints ["first","second"] JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" ); System.out.println( jsonArray3 ); // prints ["json","is","easy"]
2、Map
Map map = new HashMap(); map.put( "name", "json" ); map.put( "bool", Boolean.TRUE ); map.put( "int", new Integer(1) ); map.put( "arr", new String[]{"a","b"} ); map.put( "func", "function(i){ return this.arr[i]; }" ); JSONObject json = JSONObject.fromObject( map ); System.out.println( json ); //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true} Map map = new HashMap(); map.put( "name", "json" ); map.put( "bool", Boolean.TRUE ); map.put( "int", new Integer(1) ); map.put( "arr", new String[]{"a","b"} ); map.put( "func", "function(i){ return this.arr[i]; }" ); JSONObject json = JSONObject.fromObject( map ); System.out.println( json ); //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
3、BEAN
JSONObject jsonObject = JSONObject.fromObject( new JsonBean() ); System.out.println( jsonObject ); //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }} JSONObject jsonObject = JSONObject.fromObject( new JsonBean() ); System.out.println( jsonObject ); //{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
4、BEANS
List list = new ArrayList(); JsonBean2 jb1 = new JsonBean2(); jb1.setCol(1); jb1.setRow(1); jb1.setValue("xx"); JsonBean2 jb2 = new JsonBean2(); jb2.setCol(2); jb2.setRow(2); jb2.setValue(""); list.add(jb1); list.add(jb2); JSONArray ja = JSONArray.fromObject(list); System.out.println( ja.toString() ); //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}] List list = new ArrayList(); JsonBean2 jb1 = new JsonBean2(); jb1.setCol(1); jb1.setRow(1); jb1.setValue("xx"); JsonBean2 jb2 = new JsonBean2(); jb2.setCol(2); jb2.setRow(2); jb2.setValue(""); list.add(jb1); list.add(jb2); JSONArray ja = JSONArray.fromObject(list); System.out.println( ja.toString() ); //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
5、String to bean
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" ) ); 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" ) );
String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}"; JSONObject jsonObject = JSONObject.fromString(json); JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class ); assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) ); assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) ); assertEquals( jsonObject.get( "value" ), bean.getValue() ); String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}"; JSONObject jsonObject = JSONObject.fromString(json); JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class ); assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol()) ); assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) ); 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]; }]
上述內容就是如何進行JSON與JAVA的數據轉換,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。