マップのストレージ構造はキー/値フォームです。キーと価値は、通常のタイプ、または自分で書かれたJavabeans(この記事)、またはジェネリックを使用してリストすることができます。
(GSONのGitHubプロジェクトページ:https://github.com/google/gson)
ジャバビーン
この例では、通常のJavabeanオブジェクトの場合、JSONをtypetokenの定義に戻す方法に焦点を合わせます。
エンティティクラス:
パブリッククラスポイント{private int x;プライベートインストY; public Point(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 sety(int y){this.y = y; } @Override public String toString(){return "point [x =" + x + "、y =" + y + "]"; }}テストクラス:
java.util.linkedhashmapをインポートします。 java.util.mapをインポートします。 com.google.gson.gsonをインポートします。 com.google.gson.gsonbuilderをインポートします。 com.google.gson.reflect.typetokenをインポートします。 public class gsontest3 {public static void main(string [] args){gson gson = new gsonbuilder()。 map <point、string> map1 = new linkedhashmap <point、string>(); // linkedhashmapを使用して、結果をファーストインファーストアウトの注文map1.put(new Point(5、6)、 "a"); map1.put(new Point(8、8)、 "b");文字列s = gson.tojson(map1); System.out.println(s); // result:[{"x":5、 "y":6}、 "a"]、[{"x":8、 "y":8}、 "b"]] Map <Point、string> retmap = gson.fromjson(s、neweToken(s、New TypeToken <Map <Point、String>> for(ポイントP:retmap.keyset()){system.out.println( "key:" + p + "values:" + retmap.get(p)); } system.out.println(retmap); system.out.println( "-------------------------------"); map <string、point> map2 = new linkedhashmap <string、point>(); map2.put( "a"、new Point(3、4)); map2.put( "b"、new Point(5、6));文字列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)); }}}結果:
[[{"x":5、 "y":6}、 "a"]、[{"x":8、 "y":8}、 "b"]] key:point [x = 5、y = 6]値:キー:key:y = 8]値:b {ポイント[x = 5、y = 6] = a、ポイント[x = 8、y = 8] {"a":{"x":3、 "y":4}、 "b":{"x":5、 "y":6}} key:a values:a point [x = 3、y = 4] key:b values:point [x = 5、y = 6]]ジェネリックリスト
エンティティクラス:
Import Java.util.date;パブリッククラスの学生{private int id;プライベート文字列名;プライベートデートの誕生日; 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 date getBirthday(){return Birthday; } public void setbirthday(date Birthday){this.birthday = Birthday; } @Override public String toString(){return "desute [Birthday =" + Birthday + "、id =" + id + "、name =" + name + "]"; }} public class teacher {private int id;プライベート文字列名;プライベートストリングタイトル。 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 string getTitle(){return title; } public void settitle(string title){this.title = title; } @Override public String toString(){return "tapery [id =" + id + "、name =" + name + "、]"; }}テストクラス:
パッケージcom.tgb.lk.demo.gson.test4; java.util.arraylistをインポートします。 Import Java.util.date; java.util.linkedhashmapをインポートします。 java.util.listをインポートします。 java.util.mapをインポートします。 com.google.gson.gsonをインポートします。 com.google.gson.reflect.typetokenをインポートします。 public class gsontest4 {public static void main(string [] args){desute sustent1 = new sustent(); Student1.SetID(1); Student1.setName( "Li Kun"); Student1.setbirthday(new Date()); Student2 = new Student(); Student2.SetID(2); Student2.setName( "Cao Guisheng"); Student2.setBirthday(new Date());学生Student3 = new Student(); Student3.SetID(3); Student3.setName( "liu bo"); Student3.setbirthday(new Date());リスト<Student> Stulist = new ArrayList <Student>(); stulist.add(student1); stulist.add(student2); stulist.add(desudent3);教師教師1 = new Teacher(); Teacher1.SetID(1); Teacher1.setName( "Teacher Mi"); Teacher1.settitle( "教授");ティーチャーティーチャー2 = new Teacher(); Teacher2.SetID(2); Teacher2.setName( "Teacher Ding"); Teacher2.settitle( "講師"); list <teacher> teacherlist = new ArrayList <Teacher>(); TeacherList.Add(Teacher1); TeacherList.Add(Teacher2); map <string、object> map = new linkedhashmap <string、object>(); map.put( "学生"、stulist); Map.put( "Teachers"、TeacherList); gson gson = new gson();文字列s = gson.tojson(map); System.out.println(s); system.out.println( "-------------------------------"); map <string、object> 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( "desustor")){list <Student> stulist =(list <sustent>)retmap.get(key); System.out.Println(Stulist); } else if(key.equals( "teachers")){list <teacher> tchrlist =(list <teacher>)retmap.get(key); System.out.println(tchrlist); }}}}出力結果:
{"学生":[{"id":1、 "name": "li kun"、 "Birthday": "2012年6月9日9:48:19 pm"}、{"id":2、 "name": "cao guisheng"、 "誕生日": " 9:48:19 pm "}]、「教師」:[{" id ":1、" name ":" Teacher Mi "、" Title ":" Professor "}、{" id ":2、" name ":" Teacher Ding "、" lecturer "}]} ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------誕生日= 2012年6月9日9:48:19 pm}、{id = 3.0、name = liu bo、誕生日= 2012年6月9:48:19 pm}]キー:[{id = 1.0、name = teacher mi、professor}、{id = 2.0、name = name = teacher ding、thepesturress = recturer mi、pitturer mi、pitter {id = 2.0、name =教師ディン、title =講師}]