GsonJava对象生成Json串

由Gson将Java对象转为Json串,该对象中包含list集合和对其它对象的引用。以及Java基本数据类型。

import java.util.ArrayList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
 
 
/**Gson Java对象生成Json测试
 * @author 曙光城邦
 *
 */
public class GsonTest {
      /*
       * 创建Gson对象 ,模式为excludeFieldsWithoutExposeAnnotation 即没有@Expose注解的属性忽略
       * 常用注解:@SerializedName("className") 指定Json的键值对的Key名 
       *          @Expose  暴漏该属性。就可以序列化和反序列化。
       */
  public static void main(String[] args) {
 
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.excludeFieldsWithoutExposeAnnotation().create();
     
    /*创建老师*/
    Teacher teacher = new Teacher();
    teacher.name="张老师";
    teacher.numId=100123.23f;
     
    /*创建两个学生*/
    Student student1 = new Student();
    student1.name="张三";
    student1.age=21;
     
    Student student2 = new Student();
    student2.name="李四";
    student2.age=24;
     
    /*创建班级*/
    Class myClass = new Class();
    myClass.className="终极一班";
    myClass.classNum=101010;
    myClass.isOpen=true;
    myClass.teacher=teacher;
    myClass.students = new ArrayList<Student>();
    myClass.students.add(student1);
    myClass.students.add(student2);
     
    /*----------输出班级的Json信息*/
    String jsonString = gson.toJson(myClass);
    System.out.println(jsonString);
/* 
 * 输出的jsonString
    {
        "className":"终极一班",
        "classNum":101010,
        "isOpen":true,
        "teacher":
         {
            "name":"张老师",
            "numId":100123.23
         },
        "students":
         [
            {"name":"张三","age":21},
            {"name":"李四","age":24}
         ]
    }
*/
  }
   
      /**班级
       * @author 曙光城邦
       *
       */
    public static class Class{
        /*-------------Json基本数据类型-原始类型*/
        @SerializedName("className")@Expose
        public String className;    /*班级名称[字符串]*/
         
        @SerializedName("classNum")@Expose
        public int classNum;    /*班级编号[数字]*/
         
        @SerializedName("isOpen")@Expose
        public boolean isOpen;  /*课程是否开放 [boolean]*/
         
        @SerializedName("other")@Expose
        public String other;   /*其它信息 [null]  不赋值.为null的没输出*/
         
        /*-------------Json基本数据类型-结构类型*/
        @SerializedName("teacher")@Expose
        public Teacher teacher; /*老师 [对象]*/
         
        @SerializedName("students")@Expose
        public ArrayList<Student> students;   /*所有学生 [数组]*/
      }
   
   
      /**学生
     * @author 曙光城邦
     *
     */
    public static class Student{
          @SerializedName("name")@Expose
          public String name;
          @SerializedName("age")@Expose
          public int age;
      }
 
    /**老师
    * @author 曙光城邦
    *
    */
    public static class Teacher{
          @SerializedName("name")@Expose
          public String name;   /*老师名称*/
          @SerializedName("numId")@Expose
          public float numId;       /*老师编号*/
    }
   
}
原文地址:https://www.cnblogs.com/beenupper/p/2606984.html