透き通るGson@Expose注記、@SerializedName、解析jsonデータ


データの解析方法について説明する前に、gsonの2つの注記@Exposeと@SerializedNameについて説明します.
@Expose注記の役割:エンティティ内でシーケンス化されたくない属性を区別します.それ自体には2つの属性deserialize(逆シーケンス化)とserialize(シーケンス化)が含まれており、デフォルトはtrueです.
new GsonBuilder()を使用します.excludeFieldsWithoutExposeAnnotation().create();Gsonオブジェクトを作成します.@Exposeコメントのない属性はシーケンス化されません.
private class User{
      private int id;
      @Expose
      private String name;
      .......

}
このようにcreate()gsonオブジェクトはuserをシーケンス化し、nameという属性しか持たない.
@SerializedName注記の役割:属性のシーケンス化後の名前の定義
public class User{
     private int id;
     @Expose
     @SerializedName("username")
     private String name;
     .......

}

また、ある属性をシーケンス化しないようにtransientを使用することもできます.
private class User{
      private int id;
      private transient String name;
      .......

}

次にgsonがjsonデータをどのように解析するかを列挙する.
//   
GsonBuilder builder = new GsonBuilder();
<span style="color:#ff0000;">//       @Expose      </span>
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson = builder.create();
<span style="color:#ff0000;">//1、   string</span>
Student stu = new Student();
stu.setStudentId(333);
stu.setStudentName("qqq");
String stuStr = gson.toJson(stu);
System.out.println(stuStr); //{"studentName":"qqq","studentId":333}
<span style="color:#ff0000;">//2、string   </span>
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);
<span style="color:#ff0000;">//3、  List string</span>
List<Student> testBeanList = new ArrayList<Student>();
Student testBean = new Student();
testBean.setStudentId(555);
testBean.setStudentName("552");
testBeanList.add(testBean);
//Gson gsonList = new Gson();
Type type = new TypeToken<List<Student>>(){}.getType(); //        
String beanListToJson = gson.toJson(testBeanList, type);
System.out.println(beanListToJson); //[{"studentName":"552","studentId":555}]
<span style="color:#ff0000;">//  string   list</span>
List<Student> testBeanListFromJson = gson.fromJson(beanListToJson, type);
System.out.println(testBeanListFromJson); //[555:552]
<span style="color:#ff0000;">//4、             String</span>
List<String> testList = new ArrayList<String>();
testList.add("first");
testList.add("second");
String listToJson = gson.toJson(testList);
System.out.println(listToJson); //["first","second"]
<span style="color:#ff0000;">//5、              </span>
List<String> testList2 = (List<String>) gson.fromJson(listToJson,new TypeToken<List<String>>() {}.getType());
System.out.println(testList2);
<span style="color:#ff0000;">//6、  HashMap       JSON</span>
Map<String, String> testMap = new HashMap<String, String>();
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"}
<span style="color:#ff0000;">//7、stringMap   </span>
Map<String, String> userMap2 = (Map<String, String>) gson.fromJson(mapToJson,new TypeToken<Map<String, String>>() {}.getType());
System.out.println(userMap2); //{id=id.first, name=name.second}
<span style="color:#ff0000;">//8、        、  、map  </span>
Student user1 = new Student();
user1.setStudentId(1001);
user1.setStudentName("  ");
Student user3 = new Student();
user3.setStudentId(1002);
user3.setStudentName("  ");
Map<String, Student> userMap = new HashMap<String, Student>();
userMap.put("user1", user1);
userMap.put("user3", user3);
List<Student> userList = new ArrayList<Student>();
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<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}*/
<span style="color:#ff0000;">//9、    string   </span>
Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean,new TypeToken<Teacher>() {}.getType());
System.out.println(groupBean2);
 

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;
  }
}
 

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;
  }
}