首页 > 代码库 > 如何使用Json-lib
如何使用Json-lib
数组与List、Collection等都用JSONArray解析
boolean[] boolArray = new boolean[]{true,false,true}; JSONArray jsonArray = JSONArray.fromObject( boolArray ); System.out.println( jsonArray ); // prints [true,false,true] List list = new ArrayList(); list.add( "first" ); list.add( "second" ); JSONArray jsonArray = JSONArray.fromObject( list ); System.out.println( jsonArray ); // prints ["first","second"] JSONArray jsonArray = JSONArray.fromObject( "[‘json‘,‘is‘,‘easy‘]" ); System.out.println( jsonArray ); // prints ["json","is","easy"]
2. Beans & Maps 转换 to JSON
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 jsonObject = JSONObject.fromObject( map ); System.out.println( jsonObject ); // prints ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){ return this.arr[i]; }] class MyBean{ 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];"); // getters & setters ... } JSONObject jsonObject = JSONObject.fromObject( new MyBean() ); System.out.println( jsonObject ); /* prints {"name":"json","pojoId":1,"options":["a","f"], "func1":function(i){ return this.options[i];}, "func2":function(i){ return this.options[i];}} */
3.From JSON to Beans
Convert to DynaBean:
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}"; JSONObject jsonObject = JSONObject.fromObject( 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" ) ); Assertions.assertListEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );
Convert to Bean:
String json = "{bool:true,integer:1,string:\"json\"}"; JSONObject jsonObject = JSONObject.fromObject( json ); BeanA bean = (BeanA) JSONObject.toBean( jsonObject, BeanA.class ); assertEquals( jsonObject.get( "bool" ), Boolean.valueOf( bean.isBool() ) ); assertEquals( jsonObject.get( "integer" ), new Integer( bean.getInteger() ) ); assertEquals( jsonObject.get( "string" ), bean.getString() );
Working with XML
From JSON to XML
JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}"); String xml = XMLSerializer.write( json );
From XML to JSON
JSONArray json = (JSONArray) XMLSerializer.read( xml ); System.out.println( json ); // prints [function(i,j){ return matrix[i][j]; }]
如何使用Json-lib
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。