考虑引入gson,测试整理了工具类:
对于List,Map的泛型解析,借鉴:
http://www.blogjava.net/brock/archive/2012/08/01/384520.html
http://www.cnblogs.com/qq78292959/p/3781808.html
public class JsonUtils {
public static String toJson(Object arg) {
Gson gson = new Gson();
return gson.toJson(arg);
}
public static <T> T getObjectFromJson(String json, Class<T> classOfT) {
Gson gson = new Gson();
return gson.fromJson(json, classOfT);
}
public static <T> List<T> getListFromJson(String json, Class<T> classOfT) {
Gson gson = new Gson();
Type type = type(List.class, classOfT);
return gson.fromJson(json, type);
// return gson.fromJson(json, new TypeToken<List<T>>(){}.getType()); 错误
}
public static <K, V, T> Map<K, V> getMapFromJson(String json,
Class<K> classOfK, Class<V> classOfV) {
Gson gson = new Gson();
Type type = type(Map.class, classOfK, classOfV);
return gson.fromJson(json, type);
// return gson.fromJson(json, new TypeToken<Map<K, V>>() {}.getType()); 错误,V类型不能正确解析
}
@SuppressWarnings("rawtypes")
public static ParameterizedType type(final Class raw, final Type... args) {
return new ParameterizedType() {
public Type getRawType() {
return raw;
}
public Type[] getActualTypeArguments() {
return args;
}
public Type getOwnerType() {
return null;
}
};
}
public static void main(String[] args) {
Map<String, Object> map1 = new HashMap<String, Object>();
Map<String, Object> subMap1 = new HashMap<String, Object>();
map1.put("mapa1", 1);
map1.put("mapb1", 2);
map1.put("mapc1", new Date());
subMap1.put("sub_a1", 1);
subMap1.put("sub_b1", 2);
map1.put("subMap1", subMap1);
System.out.println(JsonUtils.toJson(map1));
Map<String, Object> map2 = new HashMap<String, Object>();
Map<String, Object> subMap2 = new HashMap<String, Object>();
map2.put("mapa2", 1);
map2.put("mapb2", 2);
map2.put("mapc2", new Date());
subMap2.put("sub_a2", 1);
subMap2.put("sub_b2", 2);
map2.put("subMap2", subMap2);
B b1 = new B("b1", "b1name", map1);
B b2 = new B("b2", "b2name", map2);
List<B> bList1 = Arrays.asList(new B[] { b1, b2 });
A a1 = new A("a1", "a1name", bList1);
System.out.println(JsonUtils.toJson(a1));
B b3 = new B("b3", "b3name", map1);
B b4 = new B("b4", "b4name", map2);
List<B> bList2 = Arrays.asList(new B[] { b3, b4 });
A a2 = new A("a2", "a2name", bList2);
List<A> aList1 = Arrays.asList(new A[] { a1, a2 });
System.out.println("aList1-json:" + JsonUtils.toJson(aList1));
System.out.println("bList1-json:" + JsonUtils.toJson(bList1));
System.out.println("map2-json:" + JsonUtils.toJson(map2));
String json = "{\"id\":\"a1\",\"name\":\"a1name\",\"bList\":[{\"id\":\"b1\",\"name\":\"b1name\",\"variable\":{\"mapc1\":\"Aug 15, 2014 3:07:56 PM\",\"mapb1\":2,\"subMap1\":{\"sub_b1\":2,\"sub_a1\":1},\"mapa1\":1}},{\"id\":\"b2\",\"name\":\"b2name\",\"variable\":{\"mapc2\":\"Aug 15, 2014 3:07:56 PM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2}}]}";
A jsonA1 = JsonUtils.getObjectFromJson(json, A.class);
System.out.println(jsonA1.getId() + "--" + jsonA1.getName());
List<B> jsonbList1 = jsonA1.getbList();
for (B jsonb1 : jsonbList1) {
System.out.println(jsonb1.getId() + "--" + jsonb1.getName());
Map<String, Object> jsonbMap1 = jsonb1.getVariable();
Set<Entry<String, Object>> entrySet = jsonbMap1.entrySet();
for (Entry<String, Object> entry : entrySet) {
System.out.println(entry.getKey() + "--" + entry.getValue());
}
}
System.out.println("*********************************************");
String aList1Json = "[{\"id\":\"a1\",\"name\":\"a1name\",\"bList\":[{\"id\":\"b1\",\"name\":\"b1name\",\"variable\":{\"mapc1\":\"Aug 18, 2014 10:42:19 AM\",\"mapb1\":2,\"subMap1\":{\"sub_b1\":2,\"sub_a1\":1},\"mapa1\":1}},{\"id\":\"b2\",\"name\":\"b2name\",\"variable\":{\"mapc2\":\"Aug 18, 2014 10:42:19 AM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2}}]},{\"id\":\"a2\",\"name\":\"a2name\",\"bList\":[{\"id\":\"b3\",\"name\":\"b3name\",\"variable\":{\"mapc1\":\"Aug 18, 2014 10:42:19 AM\",\"mapb1\":2,\"subMap1\":{\"sub_b1\":2,\"sub_a1\":1},\"mapa1\":1}},{\"id\":\"b4\",\"name\":\"b4name\",\"variable\":{\"mapc2\":\"Aug 18, 2014 10:42:19 AM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2}}]}, {\"id\":\"a1\",\"name\":\"a1name\",\"bList\":[{\"id\":\"b1\",\"name\":\"b1name\",\"variable\":{\"mapc1\":\"Aug 18, 2014 10:42:19 AM\",\"mapb1\":2,\"subMap1\":{\"sub_b1\":2,\"sub_a1\":1},\"mapa1\":1}},{\"id\":\"b2\",\"name\":\"b2name\",\"variable\":{\"mapc2\":\"Aug 18, 2014 10:42:19 AM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2}}]},{\"id\":\"a2\",\"name\":\"a2name\",\"bList\":[{\"id\":\"b3\",\"name\":\"b3name\",\"variable\":{\"mapc1\":\"Aug 18, 2014 10:42:19 AM\",\"mapb1\":2,\"subMap1\":{\"sub_b1\":2,\"sub_a1\":1},\"mapa1\":1}},{\"id\":\"b4\",\"name\":\"b4name\",\"variable\":{\"mapc2\":\"Aug 18, 2014 10:42:19 AM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2}}]}]";
List<A> aJsonList1 = JsonUtils.getListFromJson(aList1Json, A.class);
int i=0;
for (A jsona1 : aJsonList1) {
System.out.println(i++ + "--" + jsona1.getId() + "--" + jsona1.getName());
List<B> a1jsonbList1 = jsona1.getbList();
for (B jsonb1 : a1jsonbList1) {
System.out.println(jsonb1.getId() + "--" + jsonb1.getName());
Map<String, Object> jsonbMap1 = jsonb1.getVariable();
Set<Entry<String, Object>> entrySet = jsonbMap1.entrySet();
for (Entry<String, Object> entry : entrySet) {
String key = entry.getKey();
Object value = entry.getValue();
if (key.equalsIgnoreCase("subMap1") || key.equalsIgnoreCase("subMap2")){
Map<String, Object> jsonSubMap = (Map<String, Object>)value;
Set<Entry<String, Object>> subMapEntrySet = jsonSubMap.entrySet();
for (Entry<String, Object> subMapEntry : subMapEntrySet) {
System.out.println("submap:"+subMapEntry.getKey() + "--" + subMapEntry.getValue());
}
} else {
System.out.println(key + "--" + value);
}
}
}
}
String bList1Json = "[{\"id\":\"b1\",\"name\":\"b1name\",\"variable\":{\"mapc1\":\"Aug 15, 2014 4:05:30 PM\",\"mapb1\":2,\"subMap1\":{\"sub_b1\":2,\"sub_a1\":1},\"mapa1\":1}},{\"id\":\"b2\",\"name\":\"b2name\",\"variable\":{\"mapc2\":\"Aug 15, 2014 4:05:30 PM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2}}]";
System.out.println("=============================================");
List<B> bJsonList1 = JsonUtils.getListFromJson(bList1Json, B.class);
for (B jsonb1 : bJsonList1) {
System.out.println(jsonb1.getId() + "--" + jsonb1.getName());
Map<String, Object> jsonbMap1 = jsonb1.getVariable();
Set<Entry<String, Object>> entrySet = jsonbMap1.entrySet();
for (Entry<String, Object> entry : entrySet) {
System.out.println(entry.getKey() + "--" + entry.getValue());
}
}
String map2JsonA = "{\"a1\":{\"id\":\"a1\",\"name\":\"a1name\",\"bList\":[{\"id\":\"b1\",\"name\":\"b1name\",\"variable\":{\"mapc1\":\"Aug 18, 2014 10:42:19 AM\",\"mapb1\":2,\"subMap1\":{\"sub_b1\":2,\"sub_a1\":1},\"mapa1\":1}},{\"id\":\"b2\",\"name\":\"b2name\",\"variable\":{\"mapc2\":\"Aug 18, 2014 10:42:19 AM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2}}]}, \"a2\":{\"id\":\"a1\",\"name\":\"a1name\"}}";
System.out.println("++++++++++++++++++++map嵌套list,定义类对应map,才能正确解析list++++++++++++++++++++++++");
Map<String, A> map2FromJsonA = JsonUtils.getMapFromJson(map2JsonA, String.class, A.class);
Set<Entry<String, A>> map2EntrySetA = map2FromJsonA.entrySet();
for (Entry<String, A> entry : map2EntrySetA) {
A a = entry.getValue();
System.out.println(entry.getKey() + "--" + a.getId() + "--" + a.getName());
List<B> map2JsonAbList = a.getbList();
if (map2JsonAbList != null) {
for (B jsonb1 : map2JsonAbList) {
System.out.println(jsonb1.getId() + "--" + jsonb1.getName());
Map<String, Object> jsonbMap1 = jsonb1.getVariable();
Set<Entry<String, Object>> entrySet = jsonbMap1.entrySet();
for (Entry<String, Object> entryb : entrySet) {
System.out.println(entryb.getKey() + "--" + entryb.getValue());
}
}
}
}
String map2Json = "{\"mapc2\":\"Aug 18, 2014 10:20:46 AM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2,\"bList\":[{\"id\":\"b1\",\"name\":\"b1name\",\"variable\":{\"mapc1\":\"Aug 15, 2014 3:07:56 PM\",\"mapb1\":2,\"subMap1\":{\"sub_b1\":2,\"sub_a1\":1},\"mapa1\":1}},{\"id\":\"b2\",\"name\":\"b2name\",\"variable\":{\"mapc2\":\"Aug 15, 2014 3:07:56 PM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2}}]}";
System.out.println("------------------map嵌套list,定义类对应map,才能正确解析list---------------------------");
C c = JsonUtils.getObjectFromJson(map2Json, C.class);
// Map<String, Object> map2FromJson = JsonUtils.getMapFromJson(map2Json, String.class, Object.class);
// Set<Entry<String, Object>> map2EntrySet = map2FromJson.entrySet();
// for (Entry<String, Object> entry : map2EntrySet) {
// System.out.println(entry.getKey() + "--" + entry.getValue());
// }
Map<String, Integer> subMap2FromJson = c.getSubMap2();
Set<Entry<String, Integer>> subMap2EntrySet = subMap2FromJson.entrySet();
for (Entry<String, Integer> entry : subMap2EntrySet) {
System.out.println(entry.getKey() + "--" + entry.getValue());
}
List<B> map2JsonbList1 = c.getbList();
for (B jsonb1 : map2JsonbList1) {
System.out.println(jsonb1.getId() + "--" + jsonb1.getName());
Map<String, Object> jsonbMap1 = jsonb1.getVariable();
Set<Entry<String, Object>> entrySet = jsonbMap1.entrySet();
for (Entry<String, Object> entry : entrySet) {
System.out.println(entry.getKey() + "--" + entry.getValue());
}
}
map2Json = "{\"mapc2\":\"Aug 18, 2014 10:20:46 AM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2,\"bList\":[{\"id\":\"b1\",\"name\":\"b1name\",\"variable\":{\"mapc1\":\"Aug 15, 2014 3:07:56 PM\",\"mapb1\":2,\"subMap1\":{\"sub_b1\":2,\"sub_a1\":1},\"mapa1\":1}},{\"id\":\"b2\",\"name\":\"b2name\",\"variable\":{\"mapc2\":\"Aug 15, 2014 3:07:56 PM\",\"subMap2\":{\"sub_b2\":2,\"sub_a2\":1},\"mapa2\":1,\"mapb2\":2}}]}";
System.out.println("---------------------------------------------");
Map<String, Object> map2FromJson = JsonUtils.getMapFromJson(map2Json, String.class, Object.class);
Set<Entry<String, Object>> map2EntrySet = map2FromJson.entrySet();
for (Entry<String, Object> entry : map2EntrySet) {
System.out.println(entry.getKey() + "--" + entry.getValue());
}
Map<String, Object> subMap2FromJson2 = (Map<String, Object>)map2FromJson.get("subMap2");
Set<Entry<String, Object>> subMap2EntrySet2 = subMap2FromJson2.entrySet();
for (Entry<String, Object> entry : subMap2EntrySet2) {
System.out.println(entry.getKey() + "--" + entry.getValue());
}
// Map<String, B> map2FromJson2 = JsonUtils.getMapFromJson(map2Json, String.class, B.class);
// List<B> map2bListFromJson = (List<B>)map2FromJson2.get("bList");
// for (B jsonb1 : map2bListFromJson) {
// System.out.println(jsonb1.getId() + "--" + jsonb1.getName());
// Map<String, Object> jsonbMap1 = jsonb1.getVariable();
// Set<Entry<String, Object>> entrySet = jsonbMap1.entrySet();
// for (Entry<String, Object> entry : entrySet) {
// System.out.println(entry.getKey() + "--" + entry.getValue());
// }
// }
}
static class C {
private Date mapc2;
private Map<String, Integer> subMap2;
private Integer mapa2;
private Integer mapb2;
private List<B> bList;
public Date getMapc2() {
return mapc2;
}
public void setMapc2(Date mapc2) {
this.mapc2 = mapc2;
}
public Map<String, Integer> getSubMap2() {
return subMap2;
}
public void setSubMap2(Map<String, Integer> subMap2) {
this.subMap2 = subMap2;
}
public Integer getMapa2() {
return mapa2;
}
public void setMapa2(Integer mapa2) {
this.mapa2 = mapa2;
}
public Integer getMapb2() {
return mapb2;
}
public void setMapb2(Integer mapb2) {
this.mapb2 = mapb2;
}
public List<B> getbList() {
return bList;
}
public void setbList(List<B> bList) {
this.bList = bList;
}
}
static class A {
private String id;
private String name;
private List<B> bList;
public A() {
}
public A(String id, String name, List<B> bList) {
this.id = id;
this.name = name;
this.bList = bList;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<B> getbList() {
return bList;
}
public void setbList(List<B> bList) {
this.bList = bList;
}
}
static class B {
private String id;
private String name;
private Map<String, Object> variable;
public B() {
}
public B(String id, String name, Map<String, Object> variable) {
this.id = id;
this.name = name;
this.variable = variable;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, Object> getVariable() {
return variable;
}
public void setVariable(Map<String, Object> variable) {
this.variable = variable;
}
}
}
对于List,Map的泛型解析,借鉴:
http://www.blogjava.net/brock/archive/2012/08/01/384520.html
http://www.cnblogs.com/qq78292959/p/3781808.html