json lib处理json与java之间转换

通常采用json-lib来处理java与json之间的转换。
下载地址为:
http://json-lib.sourceforge.net/
两者间的数据转换为:

[list]
[*]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
[/list]

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转为BeanA
setClassMap() // {key, class} //key中对应的类
setArrayMode() //设置JSONArray转成的形式有三种 MODE_OBJECT_ARRAY, MODE_LIST or MODE_SET
morpherRegistry.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); =》 xmlStr
new XMLSerializer().read(xmlStr); =》josnobject
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值