当前位置: 代码迷 >> JavaScript >> JSON-lib.jart包引见
  详细解决方案

JSON-lib.jart包引见

热度:129   发布时间:2012-10-25 10:58:57.0
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 PropertyFilter() {
??? public boolean apply(Object source, String name, Object value) {
???? if (value != null && Number.class.isAssignableFrom(value.getClass())) {
????? return true;
???? }
???? return false;
??? }
?? });
?? JSONObject json = JSONObject.fromObject(map, jsonConfig);
?? System.out.println(json);
输出:{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"bool":true,"name":"json"}
和上面的输出:{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"bool":true,"name":"json"}
它少了:"int":1,这段,这就是属性过滤器发挥了作用,看代码就可以知道它把值为Number型的都给过滤掉了。

实例2:
如果我们把上例中的Number.class.isAssignableFrom(value.getClass())中的Number改为String呢?
?? 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 PropertyFilter() {
??? public boolean apply(Object source, String name, Object value) {
???? if (value != null && String.class.isAssignableFrom(value.getClass())) {//这里是过滤的关键
????? return true;
???? }
???? return false;
??? }
?? });
?? JSONObject json = JSONObject.fromObject(map, jsonConfig);
?? System.out.println(json);
输出:{"arr":["a","b"],"int":1,"bool":true}//它把Value的类型为String的都给过滤掉了。

6.Json-Java相互转换过滤器--同5相反这次--把json-转换为java时属性的过滤,下面我们以4中的实例1来做个演示:
实例1:
?? String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
?? JSONObject jsonObject = JSONObject.fromObject(json);

?? JsonConfig jsonConfig = new JsonConfig();
?? jsonConfig.setRootClass(Map.class);
?? jsonConfig.setJavaPropertyFilter(new PropertyFilter() {
??? public boolean apply(Object source, String name, Object value) {
???? if ("bool".equals(name) || "double".equals(name)) {//这里是过滤的关键
????? return true;
???? }
???? return false;
??? }
?? });
?? Object bean = JSONObject.toBean(jsonObject, jsonConfig);
?? System.out.println(bean);
输出:{func=function(a){ return a; }, int=1, name=json, array=[1, 2]}
同4中的实例1输出:{double=2.2, func=function(a){ return a; }, int=1, name=json, bool=true, array=[1, 2]}
少了:double=2.2, bool=true,因为name为bool和double的项已经被过滤掉了。

实例2:
?? String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
?? JSONObject jsonObject = JSONObject.fromObject(json);
?? JsonConfig jsonConfig = new JsonConfig();
?? jsonConfig.setRootClass(Map.class);
?? jsonConfig.setJavaPropertyFilter(new PropertyFilter() {
??? public boolean apply(Object source, String name, Object value) {
???? if (value != null && String.class.isAssignableFrom(value.getClass())) {// 这里是过滤的关键
????? return true;
???? }
???? return false;
??? }
?? });
?? Object bean = JSONObject.toBean(jsonObject, jsonConfig);
?? System.out.println(bean);
输出:{double=2.2, func=function(a){ return a; }, int=1, bool=true, array=[1, 2]}

由此可见,无论是java转换为json还是json转换为java,过滤器都可以根据name和value来过滤。

  相关解决方案