日期:2014-05-16  浏览次数:20292 次

JSON-lib.jart包介绍

基于json-lib.jar包Json程序,本篇主要介绍一个简单的实例!
1.首先Json-lib 需要至少有下列几个jar包的支持
jakarta commons-lang 2.4
jakarta commons-beanutils 1.7.0
jakarta commons-collections 3.2
jakarta commons-logging 1.1.1
ezmorph 1.0.6

2.Java集合类型arrays,collections同JSONArray的转换
示例1:
?? boolean[] boolArray = new boolean[] { true, false, true };
?? JSONArray jsonArray = JSONArray.fromObject(boolArray);
?? System.out.println(jsonArray);
输出:[true,false,true]

示例2:
?? List list = new ArrayList();??
?? list.add( "first" );??
?? list.add( "second" );??
?? JSONArray jsonArray = JSONArray.fromObject( list );??
?? System.out.println( jsonArray );
输出:["first","second"]

示例3:
?? JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']" );??
?? System.out.println( jsonArray );
输出:["json","is","easy"]

3.Java对象类型JavaBean,Maps同JSONObject的转换
示例1:
?? 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 );
输出:{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"bool":true,"name":"json"}

示例2:
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 );??
输出:{"func1":function(i){ return this.options[i]; },"func2":function(i){ return this.options[i]; },"name":"json","options":["a","f"],"pojoId":1}

4. JSON数据对象格式转换为JAVA类型的Beans
示例1(转换为动态的bean):
?? 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"));
输出:junit测试显示为绿条,即值相等。

示例2(转换为具体的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());
注:BeanA是具体相关属性的getters & setters方法的具体javaBean

5.Java-Json相互转换过滤器--把java类型转换为json时属性的过滤,下面我们再以3中的实例1来做个演示:
实例1:
?? 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]; }");

?? JsonConfig jsonConfig = new JsonConfig();
?? jsonConfig.setJsonPropertyFilter(new Pr