Atitit.json类库的设计与实现 ati json lib html
1. 目前jsonlib库可能有问题,可能版本冲突,抛出ex1java
2. 解决之道:1json
2.1. 自定义json解析库,使用多个复合的json 解析复合的引擎1ui
3. 几个要点2this
3.1. 复合结构的转换2google
3.2. 没有get set方法的属性自动忽略,而不抛出异常2spa
3.3. 时间格式的转换2.net
3.4. Api使用json lib的,以及gson的也一个2设计
4. 普通的的json解析器 Jackson类库 Google Gson JSON-lib类库3
5.1. toJson xxx.fromObject(x).toString(2)3
5.2. 字符串转成对象 T fromJson(String str, Class<T> type) fromObject(object)3
做者:: 老哇的爪子 Attilax 艾龙, EMAIL:1466519819@qq.com
转载请注明来源: http://blog.csdn.net/attilax
一、如,一个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就能够用这种方式解决。
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);
}
}