Atitit.json类库的设计与实现 ati json lib
Atitit.json類庫的設計與實現?ati?json?lib?
?
?
1.?目前jsonlib庫可能有問題,可能版本沖突,拋出ex1
2.?解決之道:1
2.1.?自定義json解析庫,使用多個復合的json?解析復合的引擎1
3.?幾個要點2
3.1.?復合結構的轉換2
3.2.?沒有get?set方法的屬性自動忽略,而不拋出異常2
3.3.?時間格式的轉換2
3.4.?Api使用json?lib的,以及gson的也一個2
3.5.?如果要將泛型轉換成json, 2
4.?普通的的json解析器?Jackson類庫?Google?Gson?JSON-lib類庫3
5.?Api3
5.1.?toJson???????xxx.fromObject(x).toString(2)3
5.2.?字符串轉成對象???T?fromJson(String?str,?Class<T>?type)??fromObject(object)3
6.?Teste?code3
?
?
?
1.?目前jsonlib庫可能有問題,可能版本沖突,拋出ex
2.?解決之道:
2.1.?自定義json解析庫,使用多個復合的json?解析復合的引擎
?
?
3.?幾個要點
3.1.?復合結構的轉換
3.2.?沒有get?set方法的屬性自動忽略,而不拋出異常
3.3.?時間格式的轉換
3.4.?Api使用json?lib的,以及gson的也一個
?作者::?老哇的爪子?Attilax?艾龍,??EMAIL:1466519819@qq.com
轉載請注明來源:?http://blog.csdn.net/attilax
?
3.5.?如果要將泛型轉換成json,
1、如,一個Map是這樣的,Map<String,?Map<String,?List<String>>>?map,如果這樣使用gson.toJson(map)得不到正確的結果的話,那么,我們可能要這樣使用,gson.toJson(map,new?TypeToken<Map<String,?Map<String,?List<String>>>>(){}.getType())。
?
?
解決方案:
1、如果涉及到關聯屬性的對象,轉換前可以先把它的關聯屬性轉換并放入一個新建的Map或者List,然后按照層次結構重復這樣的操作一層一層的往上轉,這樣,一般可以解決問題,但是,如果關聯的層次比較深,做起來就很麻煩了。上面提到的問題3就可以用這種方式解決。
?
?
?
4.?普通的的json解析器?Jackson類庫?Google?Gson?JSON-lib類庫
?
?
5.?Api??
5.1.?toJson???????xxx.fromObject(x).toString(2)
5.2.?字符串轉成對象???T?fromJson(String?str,?Class<T>?type)??fromObject(object)
?
6.?Teste?code
?
package?com.attilax.json;
?
import?java.util.Date;
import?java.util.HashMap;
import?java.util.Map;
?
import?com.attilax.core;
import?com.google.gson.Gson;
?
public?class?JSONObject?{
?
public?JSONObject(Map?m)?{
this.obj?=?m;
}
?
public?static?void?main(String[]?args)?{
?
Map?m?=?new?HashMap();
m.put("d",?new?Date());
//?m.put(key,?value)
?
JSONObject.fromObject(m).toString(2);
}
?
private?String?toString(int?i)?{
?
//?if(i==2)?//fmt
{
try?{
return?net.sf.json.JSONObject.fromObject(this.obj).toString(i);
}?catch?(Exception?e)?{
?
try?{
//?創建一個gson對象
Gson?gson?=?new?Gson();
?
//?轉換成json
String?json?=?gson.toJson(this.obj);
return?json;
}?catch?(Exception?e2)?{
return?JsonUtil4jackjson.buildNormalBinder().toJson(
this.obj);
}
?
}
?
}
?
}
?
public?Object?obj;
?
private?static?JSONObject?fromObject(Map?m)?{
//?TODO?Auto-generated?method?stub
return?new?JSONObject(m);
}
?
}
?
?
?
?
總結
以上是生活随笔為你收集整理的Atitit.json类库的设计与实现 ati json lib的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么用Word制作个性信封
- 下一篇: SMax4pnp.exe是什么