转自:http://smallbee.iteye.com/blog/1066993
- //转换器
- GsonBuilderbuilder=newGsonBuilder();
- //不转换没有@Expose注解的字段
- builder.excludeFieldsWithoutExposeAnnotation();
- Gsongson=builder.create();
- //1、对象转string
- Studentstu=newStudent();
- stu.setStudentId(333);
- stu.setStudentName("qqq");
- StringstuStr=gson.toJson(stu);
- System.out.println(stuStr);//{"studentName":"qqq","studentId":333}
- //2、string转对象
- Studentuser2=gson.fromJson(stuStr,Student.class);
- System.out.println(user2);
- StringstuTemp="{\"studentName\":\"qqq2\",\"studentId\":3335}";
- Studentuser4=gson.fromJson(stuTemp,Student.class);
- System.out.println(user4);
- //3、对象List转string
- List<Student>testBeanList=newArrayList<Student>();
- StudenttestBean=newStudent();
- testBean.setStudentId(555);
- testBean.setStudentName("552");
- testBeanList.add(testBean);
- //GsongsonList=newGson();
- Typetype=newTypeToken<List<Student>>(){}.getType();//指定集合对象属性
- StringbeanListToJson=gson.toJson(testBeanList,type);
- System.out.println(beanListToJson);//[{"studentName":"552","studentId":555}]
- //集合string转对象list
- List<Student>testBeanListFromJson=gson.fromJson(beanListToJson,type);
- System.out.println(testBeanListFromJson);//[555:552]
- //4、集合如果不指定类型默认为String
- List<String>testList=newArrayList<String>();
- testList.add("first");
- testList.add("second");
- StringlistToJson=gson.toJson(testList);
- System.out.println(listToJson);//["first","second"]
- //5、集合字符串转回来需要指定类型
- List<String>testList2=(List<String>)gson.fromJson(listToJson,
- newTypeToken<List<String>>(){
- }.getType());
- System.out.println(testList2);
- //6、将HashMap字符串转换为JSON
- Map<String,String>testMap=newHashMap<String,String>();
- testMap.put("id","id.first");
- testMap.put("name","name.second");
- StringmapToJson=gson.toJson(testMap);
- System.out.println(mapToJson);//{"id":"id.first","name":"name.second"}
- //7、stringMap转对象
- Map<String,String>userMap2=(Map<String,String>)gson.fromJson(mapToJson,
- newTypeToken<Map<String,String>>(){
- }.getType());
- System.out.println(userMap2);//{id=id.first,name=name.second}
- //8、对象含有普通对象、集合、map情况
- Studentuser1=newStudent();
- user1.setStudentId(1001);
- user1.setStudentName("张三");
- Studentuser3=newStudent();
- user3.setStudentId(1002);
- user3.setStudentName("李四");
- Map<String,Student>userMap=newHashMap<String,Student>();
- userMap.put("user1",user1);
- userMap.put("user3",user3);
- List<Student>userList=newArrayList<Student>();
- userList.add(user1);
- userList.add(user3);
- TeachergroupBean=newTeacher();
- groupBean.setStudent(user1);
- groupBean.setStus(userList);
- groupBean.setMap((HashMap)userMap);
- //groupBean.setUserList(userList);
- GsongsonGroup=newGson();
- StringsGroupBean=gsonGroup.toJson(groupBean,newTypeToken<Teacher>(){
- }.getType());
- System.out.println(sGroupBean);
- /*{"stus":[{"studentName":"张三","studentId":1001},{"studentName":"李四","studentId":1002}],"student":{"studentName":"张三","studentId":1001},"map":{"user3":{"studentName":"李四","studentId":1002},"user1":{"studentName":"张三","studentId":1001}},"id":0,"age":0}*/
- //9、复杂对象string转对象
- TeachergroupBean2=(Teacher)gson.fromJson(sGroupBean,
- newTypeToken<Teacher>(){
- }.getType());
- System.out.println(groupBean2);
- packagecom.andtools;
- importcom.google.gson.annotations.Expose;
- publicclassStudent{
- @Expose
- privateStringstudentName;
- @Expose
- privateintstudentId;
- publicStudent(){}
- publicStudent(intstudentId,StringstudentName){
- this.setStudentId(studentId);
- this.setStudentName(studentName);
- }
- publicStringtoString(){
- returnthis.getStudentId()+":"+this.getStudentName();
- }
- publicStringgetStudentName(){
- returnstudentName;
- }
- publicvoidsetStudentName(StringstudentName){
- this.studentName=studentName;
- }
- publicintgetStudentId(){
- returnstudentId;
- }
- publicvoidsetStudentId(intstudentId){
- this.studentId=studentId;
- }
- }
- packagecom.andtools;
- importjava.util.HashMap;
- importjava.util.List;
- importjava.util.Map;
- importcom.google.gson.annotations.Expose;
- publicclassTeacher{
- @Expose
- privateintid;
- @Expose
- privateStringname;
- @Expose
- privateintage;
- @Expose
- privateStudentstudent;
- @Expose
- privateListstus;
- @Expose
- privateHashMapmap;
- publicStringtoString(){
- returnthis.getId()+":"+this.getName()+":"+this.getAge()+":"+this.getStudent().toString()+":"+this.getStus()+":"+this.getMap();
- }
- publicintgetId(){
- returnid;
- }
- publicvoidsetId(intid){
- this.id=id;
- }
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicintgetAge(){
- returnage;
- }
- publicvoidsetAge(intage){
- this.age=age;
- }
- publicStudentgetStudent(){
- returnstudent;
- }
- publicvoidsetStudent(Studentstudent){
- this.student=student;
- }
- publicListgetStus(){
- returnstus;
- }
- publicvoidsetStus(Liststus){
- this.stus=stus;
- }
- publicHashMapgetMap(){
- returnmap;
- }
- publicvoidsetMap(HashMapmap){
- this.map=map;
- }
- }