您的位置:首页 > Web前端 > JavaScript

示例:将通过js获取的json字符串转换为Map、List集合(不太重要)

2015-04-14 12:25 771 查看
比较好的博客文章:

Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (http://blog.csdn.net/lk_blog/article/details/7685169)

Json转换利器Gson之实例二-Gson注解和GsonBuilder (http://blog.csdn.net/lk_blog/article/details/7685190)

Json转换利器Gson之实例三-Map处理(上) (http://blog.csdn.net/lk_blog/article/details/7685210)

Json转换利器Gson之实例四-Map处理(下) (http://blog.csdn.net/lk_blog/article/details/7685224)

Json转换利器Gson之实例五-实际开发中的特殊需求处理 (http://blog.csdn.net/lk_blog/article/details/7685237)

Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型 (http://blog.csdn.net/lk_blog/article/details/7685347)

简单对象转换带泛型的List转化

来源:http://blog.csdn.net/lk_blog/article/details/7685169

System.out.println("----------带泛型的List之间的转化-------------");
// 带泛型的list转化为json
String s2 = gson.toJson(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 14 12:20:17 CST 2015, id=1, name=李坤]
Student [birthDay=Tue Apr 14 12:20:17 CST 2015, id=2, name=曹贵生]
Student [birthDay=Tue Apr 14 12: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 = gson.toJson(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:" + retMap.get(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();
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 = gson.toJson(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:" + retMap.get(key));
if (key.equals("students")) {
List<Student> stuList = (List<Student>) retMap.get(key);
System.out.println(stuList);
} else if (key.equals("teachers")) {
List<Teacher> tchrList = (List<Teacher>) retMap.get(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": [
{
"id": 1,
"name": "米老师",
"title": "教授"
},
{
"id": 2,
"name": "丁老师",
"title": "讲师"
}
]
}


五 实际项目中的特殊需求处理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json
相关文章推荐