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 Java代码 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 Java代码 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 Java代码 /** * Bean.java private String name = "json"; private int pojoId = 1; private char[] options = new char[]{'a','f'}; private String func1 = "function(i){ return this.options[i]; }"; private JSONFunction func2 = new JSONFunction(new String[]{"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 Java代码 /** * private int row ; private int col ; private String value ; * */ 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 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" ) ); Java代码 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]; }] 出自http://falchion.javaeye.com/?
详细解决方案
JSON与JAVA数据的变换
热度:416 发布时间:2012-07-03 13:37:43.0
相关解决方案
- java 乱码 汉字是 ? 如何转换成汉字
- (Struts2+JSON+Ajax) XMLHttpRequest ==500如何解决
- java web 登录次数限制,该如何解决
- json 解析有关问题
- java 工商银行网银支付 B2B的 都亟需什么,需要注意什么
- Java Applet程序从JDK6升级到7时遇到的有关问题
- java web 视频相干
- 请教一个关于链接后缀的有关问题(java web 应用)
- java 系统单点登录解决方案
- java.lang.NumberFormatException: For input string: "id"该如何处理
- java.lang.NoSuchMethodException: setId([Ljava.lang.String;)解决方法
- java 获取客户端IP解决办法
- JAVA 后台怎么获得前台页面FCKedit编辑器中的内容以及内容的样式
- java 正则化匹配有关问题
- java web 受阻
- java.lang.IllegalStateException: No output folder,该怎么解决
- struts2-json中诠注@JSON(deserialize=false)是什么用?不是阻止JSON反序列化成JAVA对象吗?求解
- java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.makeAccess,该如何解决
- java 乱码有关问题 急求大神
- java EE错误如何解决阿,都是过了,没有能解决的!
- java 工程打包有关问题 多谢各位大神!多谢
- JAVA WEB导航条,该怎么处理
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password,该如何处理
- java 中Node 有关问题
- 关于 java 引述传递和值传递,你的知否
- 吐了,java Timer 终止不了。
- Java Web 学习中有关问题,请高手指教
- java web中的url地址小疑点。
- java web开发解决办法
- java.lang.NullPointerException解决方法