gson 安卓 java_gson使用在android使用例子

本文介绍了如何使用Gson库在Android中进行JSON数据的高效转换,包括对象转字符串、字符串转对象、集合操作以及处理复杂对象,如学生、教师和群组对象。Gson的使用简化了JSON处理,并展示了如何处理不同类型的数据结构。
摘要由CSDN通过智能技术生成

Java代码 15848690_1.gif15848690_2.png

15848690_3.gif

虽然android自带了json处理,但是没有封装,总觉得不方便,网上找了gson的处理,觉得还行。

虽然android自带了json处理,但是没有封装,总觉得不方便,网上找了gson的处理,觉得还行。

Java代码 15848690_1.gif15848690_2.png

15848690_3.gif

//转换器

GsonBuilder builder = newGsonBuilder();

// 不转换没有 @Expose 注解的字段

builder.excludeFieldsWithoutExposeAnnotation();

Gson gson = builder.create();

//1、对象转string

Student stu = newStudent();

stu.setStudentId(333);

stu.setStudentName("qqq");

String stuStr = gson.toJson(stu);

System.out.println(stuStr); //{"studentName":"qqq","studentId":333}

//2、string转对象

Student user2 = gson.fromJson(stuStr, Student.class);

System.out.println(user2);

String stuTemp = "{\"studentName\":\"qqq2\",\"studentId\":3335}";

Student user4 = gson.fromJson(stuTemp, Student.class);

System.out.println(user4);

//3、对象List转string

List testBeanList = newArrayList();

Student testBean = newStudent();

testBean.setStudentId(555);

testBean.setStudentName("552");

testBeanList.add(testBean);

//Gson gsonList = new Gson();

Type type = newTypeToken>(){}.getType();//指定集合对象属性

String beanListToJson = gson.toJson(testBeanList, type);

System.out.println(beanListToJson); //[{"studentName":"552","studentId":555}]

//集合string转对象list

List testBeanListFromJson = gson.fromJson(beanListToJson, type);

System.out.println(testBeanListFromJson); //[555:552]

//4、集合如果不指定类型 默认为String

List testList = newArrayList();

testList.add("first");

testList.add("second");

String listToJson = gson.toJson(testList);

System.out.println(listToJson); //["first","second"]

//5、集合字符串转回来需要指定类型

List testList2 = (List) gson.fromJson(listToJson,

newTypeToken>() {

}.getType());

System.out.println(testList2);

//6、 将HashMap字符串转换为 JSON

Map testMap = newHashMap();

testMap.put("id","id.first");

testMap.put("name","name.second");

String mapToJson = gson.toJson(testMap);

System.out.println(mapToJson); //{"id":"id.first","name":"name.second"}

//7、stringMap转对象

Map userMap2 = (Map) gson.fromJson(mapToJson,

newTypeToken>() {

}.getType());

System.out.println(userMap2); //{id=id.first, name=name.second}

//8、对象含有普通对象、集合、map情况

Student user1 = newStudent();

user1.setStudentId(1001);

user1.setStudentName("张三");

Student user3 = newStudent();

user3.setStudentId(1002);

user3.setStudentName("李四");

Map userMap = newHashMap();

userMap.put("user1", user1);

userMap.put("user3", user3);

List userList = newArrayList();

userList.add(user1);

userList.add(user3);

Teacher groupBean = newTeacher();

groupBean.setStudent(user1);

groupBean.setStus(userList);

groupBean.setMap((HashMap)userMap);

//groupBean.setUserList(userList);

Gson gsonGroup = newGson();

String sGroupBean = gsonGroup.toJson(groupBean, newTypeToken() {

}.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}*/

//转换器

GsonBuilder builder = new GsonBuilder();

// 不转换没有 @Expose 注解的字段

builder.excludeFieldsWithoutExposeAnnotation();

Gson gson = builder.create();

//1、对象转string

Student stu = new Student();

stu.setStudentId(333);

stu.setStudentName("qqq");

String stuStr = gson.toJson(stu);

System.out.println(stuStr); //{"studentName":"qqq","studentId":333}

//2、string转对象

Student user2 = gson.fromJson(stuStr, Student.class);

System.out.println(user2);

String stuTemp = "{\"studentName\":\"qqq2\",\"studentId\":3335}";

Student user4 = gson.fromJson(stuTemp, Student.class);

System.out.println(user4);

//3、对象List转string

List testBeanList = new ArrayList();

Student testBean = new Student();

testBean.setStudentId(555);

testBean.setStudentName("552");

testBeanList.add(testBean);

//Gson gsonList = new Gson();

Type type = new TypeToken>(){}.getType(); //指定集合对象属性

String beanListToJson = gson.toJson(testBeanList, type);

System.out.println(beanListToJson); //[{"studentName":"552","studentId":555}]

//集合string转对象list

List testBeanListFromJson = gson.fromJson(beanListToJson, type);

System.out.println(testBeanListFromJson); //[555:552]

//4、集合如果不指定类型 默认为String

List testList = new ArrayList();

testList.add("first");

testList.add("second");

String listToJson = gson.toJson(testList);

System.out.println(listToJson); //["first","second"]

//5、集合字符串转回来需要指定类型

List testList2 = (List) gson.fromJson(listToJson,

new TypeToken>() {

}.getType());

System.out.println(testList2);

//6、 将HashMap字符串转换为 JSON

Map testMap = new HashMap();

testMap.put("id", "id.first");

testMap.put("name", "name.second");

String mapToJson = gson.toJson(testMap);

System.out.println(mapToJson); //{"id":"id.first","name":"name.second"}

//7、stringMap转对象

Map userMap2 = (Map) gson.fromJson(mapToJson,

new TypeToken>() {

}.getType());

System.out.println(userMap2); //{id=id.first, name=name.second}

//8、对象含有普通对象、集合、map情况

Student user1 = new Student();

user1.setStudentId(1001);

user1.setStudentName("张三");

Student user3 = new Student();

user3.setStudentId(1002);

user3.setStudentName("李四");

Map userMap = new HashMap();

userMap.put("user1", user1);

userMap.put("user3", user3);

List userList = new ArrayList();

userList.add(user1);

userList.add(user3);

Teacher groupBean = new Teacher();

groupBean.setStudent(user1);

groupBean.setStus(userList);

groupBean.setMap((HashMap)userMap);

//groupBean.setUserList(userList);

Gson gsonGroup = new Gson();

String sGroupBean = gsonGroup.toJson(groupBean, new TypeToken() {

}.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}*/

Java代码 15848690_1.gif15848690_2.png

15848690_3.gif

//9、复杂对象string转对象

Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean,

newTypeToken() {

}.getType());

System.out.println(groupBean2);

//9、复杂对象string转对象

Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean,

new TypeToken() {

}.getType());

System.out.println(groupBean2);

Java代码 15848690_1.gif15848690_2.png

15848690_3.gif

packagecom.andtools;

importcom.google.gson.annotations.Expose;

publicclassStudent {

@Expose

privateString studentName;

@Expose

privateintstudentId;

publicStudent(){}

publicStudent(intstudentId,String studentName){

this.setStudentId(studentId);

this.setStudentName(studentName);

}

publicString toString(){

returnthis.getStudentId() +":"+this.getStudentName();

}

publicString getStudentName() {

returnstudentName;

}

publicvoidsetStudentName(String studentName) {

this.studentName = studentName;

}

publicintgetStudentId() {

returnstudentId;

}

publicvoidsetStudentId(intstudentId) {

this.studentId = studentId;

}

}

package com.andtools;

import com.google.gson.annotations.Expose;

public class Student {

@Expose

private String studentName;

@Expose

private int studentId;

public Student(){}

public Student(int studentId,String studentName){

this.setStudentId(studentId);

this.setStudentName(studentName);

}

public String toString(){

return this.getStudentId() + ":" + this.getStudentName();

}

public String getStudentName() {

return studentName;

}

public void setStudentName(String studentName) {

this.studentName = studentName;

}

public int getStudentId() {

return studentId;

}

public void setStudentId(int studentId) {

this.studentId = studentId;

}

}

Java代码 15848690_1.gif15848690_2.png

15848690_3.gif

packagecom.andtools;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importcom.google.gson.annotations.Expose;

publicclassTeacher {

@Expose

privateintid;

@Expose

privateString name;

@Expose

privateintage;

@Expose

privateStudent student;

@Expose

privateList stus;

@Expose

privateHashMap map;

publicString toString(){

returnthis.getId()+":"+this.getName()+":"+this.getAge() +":"+this.getStudent().toString() +":"+this.getStus() +":"+this.getMap();

}

publicintgetId() {

returnid;

}

publicvoidsetId(intid) {

this.id = id;

}

publicString getName() {

returnname;

}

publicvoidsetName(String name) {

this.name = name;

}

publicintgetAge() {

returnage;

}

publicvoidsetAge(intage) {

this.age = age;

}

publicStudent getStudent() {

returnstudent;

}

publicvoidsetStudent(Student student) {

this.student = student;

}

publicList getStus() {

returnstus;

}

publicvoidsetStus(List stus) {

this.stus = stus;

}

publicHashMap getMap() {

returnmap;

}

publicvoidsetMap(HashMap map) {

this.map = map;

}

}

package com.andtools;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import com.google.gson.annotations.Expose;

public class Teacher {

@Expose

private int id;

@Expose

private String name;

@Expose

private int age;

@Expose

private Student student;

@Expose

private List stus;

@Expose

private HashMap map;

public String toString(){

return this.getId()+":"+this.getName()+":"+this.getAge() +":"+ this.getStudent().toString() + ":" + this.getStus() + ":" + this.getMap();

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Student getStudent() {

return student;

}

public void setStudent(Student student) {

this.student = student;

}

public List getStus() {

return stus;

}

public void setStus(List stus) {

this.stus = stus;

}

public HashMap getMap() {

return map;

}

public void setMap(HashMap map) {

this.map = map;

}

}

附件为网上找的文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值