La structure de stockage de la carte est la forme clé / valeur. La clé et la valeur peuvent être de types ordinaires, ou javabeans écrits par eux-mêmes (cet article), ou répertorier avec des génériques.
(Page du projet GSON's GitHub: https://github.com/google/gson)
Javabéen
Dans cet exemple, vous souhaitez vous concentrer sur la façon de convertir JSON à la définition de TypeToken lorsqu'un objet Javabean normal.
Classe d'entité:
Point de classe publique {private int x; privé int y; Point public (int x, int y) {this.x = x; this.y = y; } public int getX () {return x; } public void setx (int x) {this.x = x; } public int gety () {return y; } public void sey (int y) {this.y = y; } @Override public String toString () {return "Point [x =" + x + ", y =" + y + "]"; }}Classe de test:
import java.util.linkedhashmap; importation java.util.map; import com.google.gson.gson; import com.google.gson.gsonbuilder; Importer com.google.gson.reflect.Typetoken; classe publique GSonTest3 {public static void main (String [] args) {gson gson = new gsonBuilder (). ImpactiableClexMapKeySerialization () .Create (); Map <point, string> map1 = new LinkedHashMap <Point, String> (); // Utilisez LinkedHashMap pour trier les résultats dans le premier ordre de l'ordre Map1.put (nouveau point (5, 6), "A"); map1.put (nouveau point (8, 8), "b"); String s = gson.tojson (map1); System.out.println (s); // Résultat: [{"x": 5, "y": 6}, "a"], [{"x": 8, "y": 8}, "b"]] map <point, string> retmap = gson.fromjson (s, new typethen <map <point, string >> () {} .gettype ()); pour (point P: retMap.KeySet ()) {System.out.println ("Key:" + P + "Valeurs:" + retMap.get (p)); } System.out.println (retMap); System.out.println ("----------------------------------"); Map <string, point> map2 = new LinkedHashMap <String, Point> (); Map2.put ("a", nouveau point (3, 4)); Map2.put ("B", nouveau point (5, 6)); String s2 = gson.tojson (map2); System.out.println (S2); Map <string, point> retMap2 = gson.fromjson (s2, new TypeToken <map <string, point >> () {} .getType ()); for (String Key: retMap2.KeySet ()) {System.out.println ("Key:" + Key + "Values:" + retMap2.get (key)); }}}résultat:
[[{"x": 5, "y": 6}, "a"], [{"x": 8, "y": 8}, "b"]] touche: point [x = 5, y = 6] valeurs: une clé: point [x = 8, y = 8] valeurs: b {point [x = 5, y = 6] = a, point [x = 8, y = 8] {"a": {"x": 3, "y": 4}, "b": {"x": 5, "y": 6}} clé: a valeurs: point [x = 3, y = 4] key: b valeurs: point [x = 5, y = 6]Liste générique
Classe d'entité:
Importer java.util.date; Étudiant de classe publique {private int id; nom de chaîne privé; anniversaire de rendez-vous privé; public int getID () {return id; } public void setid (int id) {this.id = id; } public String getName () {Nom de retour; } public void setName (string name) {this.name = name; } Public Date getBirthday () {retour anniversaire; } public void SetBirthday (date d'anniversaire) {this.birthday = anniversaire; } @Override public String toString () {return "Student [anniversaire =" + anniversaire + ", id =" + id + ", name =" + name + "]"; }} Public Class Teacher {private int id; nom de chaîne privé; titre de chaîne privée; public int getID () {return id; } public void setid (int id) {this.id = id; } public String getName () {Nom de retour; } public void setName (string name) {this.name = name; } public String getTitle () {return title; } public void SettItle (String Title) {this.title = title; } @Override public String toString () {return "enseignant [id =" + id + ", name =" + name + ",]"; }}Classe de test:
package com.tgb.lk.demo.gson.test4; import java.util.arraylist; Importer java.util.date; import java.util.linkedhashmap; Importer java.util.list; importation java.util.map; import com.google.gson.gson; Importer com.google.gson.reflect.Typetoken; classe publique GSonTest4 {public static void main (String [] args) {Student Student1 = new Student (); Student1.setid (1); Student1.setName ("Li kun"); Student1.setBirthday (new Date ()); Student2 = New Student (); Student2.SetId (2); Student2.SetName ("Cao Guiseng"); Student2.SetBirthday (new Date ()); Student Student3 = New Student (); Student3.setid (3); Student3.SetName ("Liu Bo"); Student3.SetBirthday (new Date ()); Liste <Student> Stulist = new ArrayList <Student> (); Stulist.Add (Student1); Stulist.add (Student2); Stulist.add (Student3); Enseignant enseignant 1 = nouveau professeur (); Teacher1.setid (1); Teacher1.setName ("Teacher Mi"); Teacher1.settitle ("professeur"); Enseignant enseignant2 = nouveau professeur (); Teacher2.setid (2); Teacher2.SetName ("Teacher ding"); Teacher2.SetTitle ("professeur"); List <eacher> TeacherList = new ArrayList <Seacher> (); TeacherList.Add (Teacher1); TeacherList.Add (Teacher2); Map <string, objet> map = new LinkedHashMap <String, objet> (); map.put ("étudiants", Stulist); map.put ("Teachers", TeacherList); Gson gson = new gson (); String s = gson.tojson (map); System.out.println (s); System.out.println ("----------------------------------"); Map <string, objet> retmap = gson.fromjson (s, new typetoken <map <string, list <object>>> () {} .getType ()); for (String Key: retMap.KeySet ()) {System.out.println ("Key:" + Key + "Values:" + retMap.get (key)); if (key.equals ("Students")) {list <Student> stulist = (list <Student>) retMap.get (key); System.out.println (Stulist); } else if (key.equals ("Teachers")) {list <eacher> tchrlist = (list <eacher>) retMap.get (key); System.out.println (tchrlist); }}}}Résultat de sortie:
{"étudiants": [{"id": 1, "nom": "li kun", "anniversaire": "22 juin 2012 9:48:19 PM"}, {"id": 2, "nom": "Cao Guiseng", "anniversaire": "22 juin" 9:48:19 PM "}]," Teachers ": [{" ID ": 1," Name ":" Teacher Mi "," Title ":" Professor "}, {" ID ": 2," Name ":" Teacher Ding "," Title ":" Conférencier "}]} ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- anniversaire = 22 juin 2012 9:48:19 PM}, {id = 3.0, name = liu bo, anniversaire = 22 juin 2012 9:48:19 PM}] Clé: les valeurs des enseignants: [{id = 1.0, nom = enseignant mi, titre = professeur}, {id = 2.0, name = enseignante, title = prudence}] [{id = 1.0, nom = professeur Mi, Title = Profear {id = 2.0, name = Teacher ding, title = conférencier}]