六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 29|回复: 0

json lib处理json与java之间转换

[复制链接]

升级  72%

8

主题

8

主题

8

主题

童生

Rank: 1

积分
36
 楼主| 发表于 2013-2-3 10:38:02 | 显示全部楼层 |阅读模式
通常采用json-lib来处理java与json之间的转换。
下载地址为:
http://json-lib.sourceforge.net/
两者间的数据转换为:


  • JSON        <=>java
  • string<=>java.lang.String, java.lang.Character, char
  • number<=>java.lang.Number, byte, short, int, long, float, double
  • true|false<=>java.lang.Boolean, boolean
  • null        <=>null
  • function<=>net.sf.json.JSONFunction
  • array<=>net.sf.json.JSONArray (object, string, number, boolean, function)
  • object<=>net.sf.json.JSONObject


Json在json-lib中对应的对象是JSON, 他下面的三个子类,JSONObject, JSONArray, JSONNull.

无论是json还是java 对象都是通过JSONSerializer.toJSON( obj );转成为JSONObject或JSONArray. 再用 JSONSerializer.toJava( jsonObject )为java类。
public static JSON toJSON( Object object, JsonConfig jsonConfig ) {      JSON json = null;      if( object == null ){         json = JSONNull.getInstance();      }else if( object instanceof JSONString ){         json = toJSON( (JSONString) object, jsonConfig );      }else if( object instanceof String ){         json = toJSON( (String) object, jsonConfig );      }else if( JSONUtils.isArray( object ) ){         json = JSONArray.fromObject( object, jsonConfig );      }else{         try{            json = JSONObject.fromObject( object, jsonConfig );         }catch( JSONException e ){            if( object instanceof JSONTokener ){               ((JSONTokener) object).reset();            }            json = JSONArray.fromObject( object, jsonConfig );         }      }      return json;   }
返回json => json.toString()就是jsonStr,
转为java类:
JSONObject.toBean(json) or JSONArray.toArray(json)

1. Java到json转换时可通过JsonConfig来控制。
JSONSerializer.toJSON(javaobj, jsonConfig)=>jsonobj//实际方法为:json = JSONArray.fromObject( object, jsonConfig );json = JSONObject.fromObject( object, jsonConfig );setJsonPropertyFilter(PropertyFilter);  //设置哪些字段要转registerJsonBeanProcessor(class, JsonBeanProcessor); registerJsonValueProcessor(class, JsonValueProcessor); //处理Bean对象的属性值的转换

2. json-lib是通过Morpher来jsonstr转换对象,对于不知道类型的转为 MorpherDynBean。
对于复杂的对象想通过Morpher来转就比较困难。可通过jsonConfig
JSONSerializer.toJSON(str, c) => JSONObject或JSONArray再根据如下转成对象JSONSerializer.toJava(jsonObject, jsonConfig)=>Object//实际方法为:JSONObject.toBean(jsonObject, jsonConfig) ==> Object;JSONArray.toArray(jsonObject, jsonConfig) ==> Object;JSONArray.toList(jsonObject, jsonConfig) ==> List;setRootClass(bean.class);           // 设置要转成的类,会根据属性的类型来转BeanB {BeanA a;  BeanA[] as}     // 转时会将DynBean转为BeanAsetClassMap() // {key, class}          //key中对应的类setArrayMode() //设置JSONArray转成的形式有三种 MODE_OBJECT_ARRAY, MODE_LIST or MODE_SETmorpherRegistry.registerMorpher( dynaMorpher );  
如date:
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{ "yyyy-MM-dd HH:mm:ss",  "yyyy-MM-dd"          }));

在toBean()时用到Morpher:
//对象:JSONUtils.getMorpherRegistry().registerMorpher(new BeanMorpher( pd.getPropertyType(),JSONUtils.getMorpherRegistry() ) );setProperty( root, key, JSONUtils.getMorpherRegistry().morph( pd.getPropertyType(), value ), jsonConfig );//数组:ObjectArrayMorpher beanMorpher = new ObjectArrayMorpher(new BeanMorpher( innerType, JSONUtils.getMorpherRegistry() ) );JSONUtils.getMorpherRegistry().registerMorpher( beanMorpher );array = JSONUtils.getMorpherRegistry().morph( Array.newInstance( innerType, 0 ).getClass(), array );setProperty( root, key, array, jsonConfig );

与XML互换:
new XMLSerializer().write(jsonObject); =》 xmlStrnew XMLSerializer().read(xmlStr); =》josnobject
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表