GSON的使用方法

google GSON的使用方法

同样是以流的方式解析,效率不错。

Java代码    
1.	//转换器 
2.	GsonBuilder builder = new GsonBuilder(); 
3.	// 不转换没有 @Expose 注解的字段 
4.	builder.excludeFieldsWithoutExposeAnnotation(); 
5.	Gson gson = builder.create(); 
6.	
7.	//1、对象转string 
8.	Student stu = new Student(); 
9.	stu.setStudentId(333); 
10.	stu.setStudentName("qqq"); 
11.	String stuStr = gson.toJson(stu); 
12.	System.out.println(stuStr); //{"studentName":"qqq","studentId":333} 
13.	
14.	
15.	//2、string转对象 
16.	Student user2 = gson.fromJson(stuStr, Student.class); 
17.	System.out.println(user2); 
18.	String stuTemp = "{\"studentName\":\"qqq2\",\"studentId\":3335}"; 
19.	Student user4 = gson.fromJson(stuTemp, Student.class); 
20.	System.out.println(user4); 
21.	
22.	//3、对象List转string 
23.	List<Student> testBeanList = new ArrayList<Student>(); 
24.	Student testBean = new Student(); 
25.	testBean.setStudentId(555); 
26.	testBean.setStudentName("552"); 
27.	testBeanList.add(testBean); 
28.	
29.	//Gson gsonList = new Gson(); 
30.	Type type = new TypeToken<List<Student>>(){}.getType(); //指定集合对象属性 
31.	String beanListToJson = gson.toJson(testBeanList, type); 
32.	System.out.println(beanListToJson); //[{"studentName":"552","studentId":555}] 
33.	
34.	//集合string转对象list 
35.	List<Student> testBeanListFromJson = gson.fromJson(beanListToJson, type); 
36.	System.out.println(testBeanListFromJson); //[555:552] 
37.	
38.	//4、集合如果不指定类型 默认为String 
39.	List<String> testList = new ArrayList<String>(); 
40.	testList.add("first"); 
41.	testList.add("second"); 
42.	String listToJson = gson.toJson(testList); 
43.	System.out.println(listToJson); //["first","second"] 
44.	
45.	//5、集合字符串转回来需要指定类型 
46.	List<String> testList2 = (List<String>) gson.fromJson(listToJson, 
47.	new TypeToken<List<String>>() { 
48.	}.getType()); 
49.	System.out.println(testList2); 
50.	
51.	//6、 将HashMap字符串转换为 JSON 
52.	Map<String, String> testMap = new HashMap<String, String>(); 
53.	testMap.put("id", "id.first"); 
54.	testMap.put("name", "name.second"); 
55.	String mapToJson = gson.toJson(testMap); 
56.	System.out.println(mapToJson); //{"id":"id.first","name":"name.second"} 
57.	//7、stringMap转对象 
58.	Map<String, String> userMap2 = (Map<String, String>) gson.fromJson(mapToJson, 
59.	new TypeToken<Map<String, String>>() { 
60.	}.getType()); 
61.	System.out.println(userMap2); //{id=id.first, name=name.second} 
62.	
63.	//8、对象含有普通对象、集合、map情况 
64.	Student user1 = new Student(); 
65.	user1.setStudentId(1001); 
66.	user1.setStudentName("张三"); 
67.	
68.	Student user3 = new Student(); 
69.	user3.setStudentId(1002); 
70.	user3.setStudentName("李四"); 
71.	
72.	Map<String, Student> userMap = new HashMap<String, Student>(); 
73.	userMap.put("user1", user1); 
74.	userMap.put("user3", user3); 
75.	
76.	List<Student> userList = new ArrayList<Student>(); 
77.	userList.add(user1); 
78.	userList.add(user3); 
79.	
80.	Teacher groupBean = new Teacher(); 
81.	groupBean.setStudent(user1); 
82.	groupBean.setStus(userList); 
83.	groupBean.setMap((HashMap)userMap); 
84.	//groupBean.setUserList(userList); 
85.	Gson gsonGroup = new Gson(); 
86.	
87.	String sGroupBean = gsonGroup.toJson(groupBean, new TypeToken<Teacher>() { 
88.	}.getType()); 
89.	System.out.println(sGroupBean); 
90.	/*{"stus":[{"studentName":"张三","studentId":1001},{"studentName":"李四","studentId":1002}],"student":{"studentName":"张三","studentId":1001},"map":{"user3":{"studentName":"李四","studentId":1002},"user1":{"studentName":"张三","studentId":1001}},"id":0,"age":0}*/ 
Java代码    
1.	//9、复杂对象string转对象 
2.	Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean, 
3.	new TypeToken<Teacher>() { 
4.	}.getType()); 
5.	System.out.println(groupBean2); 
Java代码    
1.	package com.andtools; 
2.	
3.	import com.google.gson.annotations.Expose; 
4.	
5.	public class Student { 
6.	@Expose 
7.	private String studentName; 
8.	@Expose 
9.	private int studentId; 
10.	public Student(){} 
11.	public Student(int studentId,String studentName){ 
12.	this.setStudentId(studentId); 
13.	this.setStudentName(studentName); 
14.	} 
15.	public String toString(){ 
16.	return this.getStudentId() + ":" + this.getStudentName(); 
17.	} 
18.	public String getStudentName() { 
19.	return studentName; 
20.	} 
21.	public void setStudentName(String studentName) { 
22.	this.studentName = studentName; 
23.	} 
24.	public int getStudentId() { 
25.	return studentId; 
26.	} 
27.	public void setStudentId(int studentId) { 
28.	this.studentId = studentId; 
29.	} 
30.	
31.	} 
Java代码    
1.	package com.andtools; 
2.	
3.	import java.util.HashMap; 
4.	import java.util.List; 
5.	import java.util.Map; 
6.	
7.	import com.google.gson.annotations.Expose; 
8.	
9.	public class Teacher { 
10.	@Expose 
11.	private int id; 
12.	@Expose 
13.	private String name; 
14.	@Expose 
15.	private int age; 
16.	@Expose 
17.	private Student student; 
18.	@Expose 
19.	private List stus; 
20.	@Expose 
21.	private HashMap map; 
22.	public String toString(){ 
23.	return this.getId()+":"+this.getName()+":"+this.getAge() +":"+ this.getStudent().toString() + ":" + this.getStus() + ":" + this.getMap(); 
24.	} 
25.	public int getId() { 
26.	return id; 
27.	} 
28.	public void setId(int id) { 
29.	this.id = id; 
30.	} 
31.	public String getName() { 
32.	return name; 
33.	} 
34.	public void setName(String name) { 
35.	this.name = name; 
36.	} 
37.	public int getAge() { 
38.	return age; 
39.	} 
40.	public void setAge(int age) { 
41.	this.age = age; 
42.	} 
43.	public Student getStudent() { 
44.	return student; 
45.	} 
46.	public void setStudent(Student student) { 
47.	this.student = student; 
48.	} 
49.	public List getStus() { 
50.	return stus; 
51.	} 
52.	public void setStus(List stus) { 
53.	this.stus = stus; 
54.	} 
55.	public HashMap getMap() { 
56.	return map; 
57.	} 
58.	public void setMap(HashMap map) { 
59.	this.map = map; 
60.	} 
61.	
62.	} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值