⽰例:将通过js获取的json字符串转换为Map、List集合(不太重要)
⽐较好的博客⽂章:
Json转换利器Gson之实例⼀-简单对象转化和带泛型的List转化 ()
Json转换利器Gson之实例⼆-Gson注解和GsonBuilder ()
Json转换利器Gson之实例三-Map处理(上) ()
Json转换利器Gson之实例四-Map处理(下) ()
Json转换利器Gson之实例五-实际开发中的特殊需求处理 ()
Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型 ()
简单对象转换带泛型的List转化
来源:
System.out.println("----------带泛型的List之间的转化-------------");
/
/ 带泛型的list转化为json
String s2 = Json(list);
System.out.println("带泛型的list转化为json==" + s2);
// json转为带泛型的list
List<Student> retList = gson.fromJson(s2,
new TypeToken<List<Student>>() {
}.getType());
for (Student stu : retList) {
System.out.println(stu);
}
这都是基于Json字符串的形式,类List型
问题:怎么在不知道Json字符串形式的前提下,放⼊某种容器呢?或者根本是⽆法解决的?
⼆、有时候我们不需要把实体的所有属性都导出,只想把⼀部分属性导出为Json.(这个挺实⽤的)
有时候我们的实体类会随着版本的升级⽽修改.
有时候我们想对输出的json默认排好格式.(这个也重要)
⽅法:
//注意这⾥的Gson的构建⽅式为GsonBuilder,区别于test1中的Gson gson = new Gson();
Gson gson = new GsonBuilder()
.setPrettyPrinting() //对json字符串结果格式化.
.create();
⽽直接输出的对象就只能是连续排列的,如
// json转为带泛型的list
List<Student> retList = gson.fromJson(s2,
new TypeToken<List<Student>>() {
}.getType());
for (Student stu : retList) {
System.out.println(stu);
}
输出为
Student [birthDay=Tue Apr 1412:20:17 CST 2015, id=1, name=李坤]
Student [birthDay=Tue Apr 1412:20:17 CST 2015, id=2, name=曹贵⽣]
Student [birthDay=Tue Apr 1412:20:17 CST 2015, id=3, name=柳波]
.enableComplexMapKeySerialization() //⽀持Map的key为复杂对象的形式
(三)Map处理(上)
Json格式的字符串是很合理的,能将所有的对应信息都能打印出来。
Gson gson = new GsonBuilder().enableComplexMapKeySerialization()
.create();
// 使⽤LinkedHashMap将结果按先进先出顺序排列
Map<Point, String> map1 = new LinkedHashMap<Point, String>();
map1.put(new Point(5, 6), "a");
map1.put(new Point(8, 8), "b");
String s = Json(map1);
System.out.println(s);// 结果:[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]
//创建新的Map对象接受Json格式字符串
Map<Point, String> retMap = gson.fromJson(s,new TypeToken<Map<Point, String>>() {                }.getType());//注意这个地⽅,泛型
for (Point p : retMap.keySet())
{
System.out.println("key:" + p + " values:" + (p));
}
System.out.println(retMap);
结果为:
[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]
key:Point [x=5, y=6] values:a
key:Point [x=8, y=8] values:b
{Point [x=5, y=6]=a, Point [x=8, y=8]=b}
(四)Map(下)
public static void main(String[] args)
{
Student student1 = new Student();
student1.setId(1);
student1.setName("李坤");
student1.setBirthDay(new Date());
Student student2 = new Student();
student2.setId(2);
student2.setName("曹贵⽣");
student2.setBirthDay(new Date());
Student student3 = new Student();
student3.setId(3);
student3.setName("柳波");
student3.setBirthDay(new Date());
List<Student> stulist = new ArrayList<Student>();
stulist.add(student1);
stulist.add(student2);
stulist.add(student3);
Teacher teacher1 = new Teacher();
teacher1.setId(1);
teacher1.setName("⽶⽼师");
teacher1.setTitle("教授");
Teacher teacher2 = new Teacher();
Teacher teacher2 = new Teacher();
teacher2.setId(2);
teacher2.setName("丁⽼师");
teacher2.setTitle("讲师");
List<Teacher> teacherList = new ArrayList<Teacher>();
teacherList.add(teacher1);
teacherList.add(teacher2);
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("students", stulist);
map.put("teachers", teacherList);
Gson gson = new GsonBuilder()
.setPrettyPrinting()//对json字符串结果格式化.
.create();
String s = Json(map);
System.out.println(s);
System.out.println("----------------------------------");
//转为对象
Map<String, Object> retMap = gson.fromJson(s,
new TypeToken<Map<String, List<Object>>>() {
}.getType());
for (String key : retMap.keySet()) {
System.out.println("key:" + key + " values:" + (key));
if (key.equals("students")) {
List<Student> stuList = (List<Student>) (key);
System.out.println(stuList);
} else if (key.equals("teachers")) {
List<Teacher> tchrList = (List<Teacher>) (key);
System.out.println(tchrList);
}
}
}
输出为
{
"students": [
{
"id": 1,
"name": "李坤",
"birthDay": "Apr 14, 2015 3:44:44 PM"
},
{
"id": 2,
"name": "曹贵⽣",
"birthDay": "Apr 14, 2015 3:44:44 PM"
},
{
"id": 3,
"name": "柳波",
"birthDay": "Apr 14, 2015 3:44:44 PM"
}
],
"teachers": [
{
js获取json的key和value"id": 1,
"name": "⽶⽼师",
"title": "教授"
},
{
"id": 2,
"name": "丁⽼师",
"title": "讲师"
}
]
}
五 实际项⽬中的特殊需求处理

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。