转载

Gson用法总结

最近android开发频繁用到Gson库,传送接受json数据,以此总结一下

Student类(javabean类)

public class Student {
private int number;
private String name;
public Student(int number, String name) {
this.number = number;
this.name = name;
}
public int getNumber() { return number;}
public String getName() { return name; }
public void setNumber(int number) {this.number = number;}
public void setName(String name) {this.name = name;}
}

Teacher类(javabean类)

public class Teacher {
private String name;
private boolean sex;
public Teacher(String name, boolean sex) {
this.name = name;
this.sex = sex;
}
public String getName() {return name;}
public boolean getSex() {return sex;}
public void setName(String name) {this.name = name;}
public void setSex(boolean sex) {this.sex = sex;}
}

  • javabean对象, json字符串互相转换

    Student studnet = new Student();
    student.setName("zhouyu");
    student.setNumber(140);
    /*对象转化为字符串*/
    String jsonStudent = new Gson().toJson(student);
    /*字符串转化为对象*/
    Student student = new Gson().from(jsonStudent, Student.class);
    /* jsonStudent: {"number":140,"name":"周宇"} */

  • javabean对象数组转化为字符串

    List<Student> studentList = new ArrayList<Student>();
    Student studentOne = new Student(11, "周宇");
    Student studentTwo = new Student( 12, "周坤");
    Student studentThree= new Student(13, "周恒");
    studentList.add(studentOne);
    studentList.add(studentTwo);
    studentList.add(studentThree);
    /*对象数组转化为字符串*/
    String jsonStudentList = new Gson().toJson(studentList);
    System.out.println(jsonStudentList);
    /*将字符串转化为对象数组*/
    JsonParser parser = new JsonParser();
    JsonArray jsonArray = parser.parse(jsonStudentList).getAsJsonArray();
    Gson gson = new Gson();
    for(JsonElement element : jsonArray) {
    Student student = gson.fromJson(element, Student.class);
    System.out.println(student.getName());
    }
    /*jsonStudentList:[{"number":11,"name":"周宇"},{"number":12,"name":"周坤"},{"number":13,"name":"周恒"}] */

  • json字符串的拼接与解析

    List<Student> studentList = new ArrayList<Student>();
    List<Teacher> teacherList = new ArrayList<Teacher>();
    Student studentOne = new Student(11, "周宇");
    studentList.add(studentOne);
    Student studentTwo = new Student( 12, "周坤");
    studentList.add(studentTwo);
    Student studentThree= new Student(13, "周恒");
    studentList.add(studentThree);
    Teacher teacherOne = new Teacher("张三", true); teacherList.add(teacherOne);
    Teacher teacherTwo = new Teacher("李四", false); teacherList.add(teacherTwo);
    Teacher teacherThree = new Teacher("王五",true);
    teacherList.add(teacherThree);
    String jsonStudentList = new Gson().toJson(studentList);
    String jsonTeacherList = new Gson().toJson(teacherList);
    JSONObject objectOne = new JSONObject();
    objectOne.put("studentList", jsonStudentList);
    objectOne.put("teacherList", jsonTeacherList);
    String jsonComplex = objectOne.toString();
    JSONObject objectTwo = new JSONObject(jsonComplex);
    String jsonStudentList = objectTwo.getString("studentList");
    String jsonTeacherList = objectTwo.getString("teacherList");
正文到此结束
本文目录