【转】https://blog.csdn.net/silencecarrot/article/details/79381189
本文主要示例两部分内容:
- JavaBean、List、Map、复杂集合 转换成 json字符串;
- json字符串 转换成 JavaBean、List、Map、复杂集合;
定义POJO:
public class A {
private String usename;
private String password;
public A() {
}
public A(String usename, String password) {
this.usename = usename;
this.password = password;
}
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
开始转换测试:
public static void main(String[] args) {
//javaBean 转 json字符串
A a1 = new A("wei.hu", "123456");
String a1Json = JSON.toJSONString(a1);
System.out.println(a1Json);
//List 转 json字符串
A a2 = new A("mengna.shi", "123456");
A a3 = new A("ming.li", "567890");
List aList = Lists.newArrayList(a1, a2, a3);
String aListJson = JSON.toJSONString(aList);
System.out.println(aListJson);
//List 转 json字符串
List stringList = Lists.newArrayList("wei.hu", "mengna.shi", "fastJson");
String stringListJson = JSON.toJSONString(stringList);
System.out.println(stringListJson);
//List 转 json字符串
List integerList = Lists.newArrayList(10, 9, 8, 7);
String integerListJson = JSON.toJSONString(integerList);
System.out.println(integerListJson);
//Map 转 json字符串
Map aMap = Maps.newHashMap();
aMap.put("a1", a1);
aMap.put("a2", a2);
aMap.put("a3", a3);
String aMapJson = JSON.toJSONString(aMap);
System.out.println(aMapJson);
//Map 转 json字符串
Map stringMap = Maps.newHashMap();
stringMap.put("name", "wei.hu");
stringMap.put("gender", "man");
stringMap.put("age", "18");
String stringMapJson = JSON.toJSONString(stringMap);
System.out.println(stringMapJson);
//Map 转 json字符串
Map integerMap = Maps.newHashMap();
integerMap.put("int1",18);
integerMap.put("int2", 19);
integerMap.put("int3", 20);
String integerMapJson = JSON.toJSONString(integerMap);
System.out.println(integerMapJson);
//Map 转 json字符串
Map objectMap = Maps.newHashMap();
objectMap.put("name", "wei.hu");
objectMap.put("gender", "man");
objectMap.put("age", 18);
String objectMapJson = JSON.toJSONString(objectMap);
System.out.println(objectMapJson);
//List
输出:
{"password":"123456","usename":"wei.hu"}
[{"password":"123456","usename":"wei.hu"},{"password":"123456","usename":"mengna.shi"},{"password":"567890","usename":"ming.li"}]
["wei.hu","mengna.shi","fastJson"]
[10,9,8,7]
{"a1":{"password":"123456","usename":"wei.hu"},"a2":{"password":"123456","usename":"mengna.shi"},"a3":{"password":"567890","usename":"ming.li"}}
{"gender":"man","name":"wei.hu","age":"18"}
{"int2":19,"int1":18,"int3":20}
{"gender":"man","name":"wei.hu","age":18}
[{"a1":{"password":"123456","usename":"wei.hu"},"a2":{"password":"123456","usename":"mengna.shi"},"a3":{"password":"567890","usename":"ming.li"}},{"a1":{"$ref":"$[0].a1"},"a2":{"$ref":"$[0].a2"}}]
{"key1":[{"password":"123456","usename":"wei.hu"},{"password":"123456","usename":"mengna.shi"}],"key2":[{"$ref":"$.key1[1]"},{"password":"567890","usename":"ming.li"}]}
wei.hu / 123456
3
3
4
3
mapList.size():2
listMap1.size():2
[{"myAList2":[{"password":"28","usename":"song.xu"},{"password":"36","usename":"jielun.zhou"}],"myAList1":[{"password":"123456","usename":"wei.hu"},{"password":"abcdef","usename":"mengna.shi"},{"password":"098765","usename":"admin"}]},{"myAList3":[{"password":"61","usename":"junjie.lin"},{"password":"31","usename":"jian.xiao"},{"password":"32","usename":"xi.ben"}]},{"myAList4":[{"password":"33","usename":"xing.qun"},{"password":"34","usename":"datong.fang"},{"password":"35","usename":"dun.tong"}]}]
newList.size():3
{"newList3":[{"objectA9":{"password":"9","usename":"9"},"objectA0":{"password":"0","usename":"0"}}],"newList2":[{"objectA7":{"password":"7","usename":"7"},"objectA8":{"password":"8","usename":"8"}}],"newList1":[{"objectA2":{"password":"2","usename":"2"},"objectA3":{"password":"3","usename":"3"},"objectA1":{"password":"1","usename":"1"}},{"objectA6":{"password":"6","usename":"6"},"objectA4":{"password":"4","usename":"4"},"objectA5":{"password":"5","usename":"5"}}]}
newMap.size()3