Use the Gson library to convert the map key values in Java to the structure object to Json_java

Source: Internet
Author: User
Tags tojson

Map's storage-structured key/value form, Key and value can be ordinary types, can also be written by themselves JavaBean (this article), can also be a list with generics.

(Gson GitHub project page: Https://github.com/google/gson)

JavaBean

In this case you want to focus on the definition of how to revert json to a normal JavaBean object when TypeToken.

Entity classes:

public class Point { 
  private int x; 
  private int 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 + "]"; 
  } 
 
} 

Test class:

Import Java.util.LinkedHashMap; 
 
Import Java.util.Map; 
Import Com.google.gson.Gson; 
Import Com.google.gson.GsonBuilder; 
 
Import Com.google.gson.reflect.TypeToken; public class GsonTest3 {public static void main (string[] args) {Gson Gson = new Gsonbuilder (). Enablecomplexmap 
 
    Keyserialization (). Create (); Map<point, string> map1 = new Linkedhashmap<point, string> ();//Use Linkedhashmap to arrange the results in FIFO order Map1.put (new 
    Point (5, 6), "a"); 
    Map1.put (New Point (8, 8), "B"); 
    String s = Gson.tojson (MAP1); System.out.println (s);//Result: [[{"X": 5, "Y": 6}, "a"],[{"X": 8, "Y": 8}, "B"]] map<point, string> retmap = Gson.fromjs 
    On (S, New Typetoken<map<point, string>> () {}.gettype ()); 
    For (Point 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)); 
    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)); 

 } 
 
  } 
}

Results:

[[{"X": 5, "Y": 6}, "a"],[{"X": 8, "Y": 8}, "B"]] 
Key:point [X=5, y=6] values:a 
key:point [X=8, y=8] values:b 
{point [X=5, Y=6]=a, point [X=8, y=8]=b} 
-------- -------------------------- 
{"A": {"X": 3, "Y": 4}, "B": {"X": 5, "Y": 6}} 
key:a values:point [x=3, y=4] 
key:b Values:point [X=5, y=6] 

Generic List

Entity classes:

Import Java.util.Date; 
  public class Student {private int id; 
  private String name; 
 
  Private Date birthday; 
  public int getId () {return id; 
  The 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; 
  The public void Setbirthday (Date birthday) {this.birthday = birthday; 
        @Override public String toString () {return "Student [birthday= + birthday +", id= "+ ID +", name= " 
  + name + "]"; 
 
  } public class Teacher {private int id; 
 
  private String name; 
 
  Private String title; 
  public int getId () {return id; 
  The 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 "Teacher [id= + ID +", name= "+ name +", title= "+ title 
  + "]"; 

 } 
 
}

Test class:

Package com.tgb.lk.demo.gson.test4; 
Import java.util.ArrayList; 
Import Java.util.Date; 
Import Java.util.LinkedHashMap; 
Import java.util.List; 
 
Import Java.util.Map; 
Import Com.google.gson.Gson; 
 
Import Com.google.gson.reflect.TypeToken; 
    public class GsonTest4 {public static void main (string[] args) {Student student1 = new Student (); 
    Student1.setid (1); 
    Student1.setname ("Li Kun"); 
 
    Student1.setbirthday (New Date ()); 
    Student Student2 = new Student (); 
    Student2.setid (2); 
    Student2.setname ("Cao Guisheng"); 
 
    Student2.setbirthday (New Date ()); 
    Student Student3 = new Student (); 
    Student3.setid (3); 
    Student3.setname ("Liupo"); 
 
    Student3.setbirthday (New Date ()); 
    list<student> stulist = new arraylist<student> (); 
    Stulist.add (STUDENT1); 
    Stulist.add (Student2); 
 
    Stulist.add (STUDENT3); 
    Teacher teacher1 = new Teacher (); 
    Teacher1.setid (1); 
    Teacher1.setname ("rice teacher"); 
 
  Teacher1.settitle ("Professor");  Teacher teacher2 = new Teacher (); 
    Teacher2.setid (2); 
    Teacher2.setname ("Teacher Ding"); 
    Teacher2.settitle ("lecturer"); 
    list<teacher> teacherlist = new arraylist<teacher> (); 
    Teacherlist.add (Teacher1); 
 
    Teacherlist.add (TEACHER2); 
    map<string, object> map = new linkedhashmap<string, object> (); 
    Map.put ("Students", stulist); 
 
    Map.put ("Teachers", teacherlist); 
    Gson Gson = new Gson (); 
    String 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 ("students")) {list<student> stulist = (list<student>) retmap.get (key); 
      System.out.println (stulist); } else if (key.equals ("Teachers")) {list<teacher> tchrlist = (list<teacher>) retmap.get (key); 
      System.out.println (tchrlist); 

 } 
    } 
 
  } 
}

Output results:

{"Students": [{"id": 1, "name": "Li Kun", "Birthday": "June, 9:48:19 PM"},{"id": 2, "name": "Cao Guisheng", "Birthday": "June 22, 2012 9:48:19 pm "},{" ID: 3, "name": "Liupo", "Birthday": "June, 9:48:19 PM"}], "Teachers": [{"id": 1, "name": "M Teacher", "title": " Professor "},{" ID ": 2," name ":" Teacher Ding "," title ":" Lecturer "}]} 
---------------------------------- 
key:students values:[{id= 1.0, Name= Li Kun, Birthday=jun, 9:48:19 pm}, {id=2.0, name= Cao Gui-sheng, Birthday=jun, 9:48:19 pm}, {id=3.0, name= Liupo, b Irthday=jun 9:48:19 pm}] 
[{id=1.0, name= Li Kun, Birthday=jun, 9:48:19 PM}, {id=2.0, name= Cao Gui, Birthday=ju N, 9:48:19 pm}, {id=3.0, name= Liupo, Birthday=jun, 9:48:19 PM}] 
key:teachers values:[{id=1.0, name= rice teacher, t Professor Itle=}, {id=2.0, name= Teacher, title= Lecturer}] 
[{id=1.0, name= rice teacher, Professor Title=}, {id=2.0, name=, teacher, title= lecturer}] 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.