一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 使用GSON庫將Java中的map鍵值對應結構對象轉換為JSON

使用GSON庫將Java中的map鍵值對應結構對象轉換為JSON

2020-05-19 10:55李坤 JAVA教程

GSON是由Google開發并開源的實現Java對象與JSON之間相互轉換功能的類庫,這里我們來看一下使用GSON庫將Java中的map鍵值對應結構對象轉換為JSON的示例:

Map的存儲結構式Key/Value形式,Key 和 Value可以是普通類型,也可以是自己寫的JavaBean(本文),還可以是帶有泛型的List.

(GSON的GitHub項目頁:https://github.com/google/gson)

JavaBean

本例中您要重點看如何將Json轉回為普通JavaBean對象時TypeToken的定義.

實體類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 + "]";
  }
 
}

測試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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().enableComplexMapKeySerialization()
        .create();
 
    Map<Point, String> map1 = new LinkedHashMap<Point, String>();// 使用LinkedHashMap將結果按先進先出順序排列
    map1.put(new Point(5, 6), "a");
    map1.put(new Point(8, 8), "b");
    String s = gson.toJson(map1);
    System.out.println(s);// 結果:[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]
 
    Map<Point, String> retMap = gson.fromJson(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));
    }
 
  }
}

結果:

?
1
2
3
4
5
6
7
8
[[{"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]

泛型List

實體類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.Date;
 
public class Student {
  private int id;
  private String name;
  private Date birthDay;
 
  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 "Student [birthDay=" + birthDay + ", id=" + id + ", name="
        + name + "]";
  }
 
}
 
 
public class Teacher {
  private int id;
 
  private String name;
 
  private String title;
 
  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 "Teacher [id=" + id + ", name=" + name + ", id="codetool">

測試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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("李坤");
    student1.setBirthDay(new Date());
 
    Student student2 = new Student();
    student2.setId(2);
    student2.setName("曹貴生");
    student2.setBirthDay(new Date());
 
    Student student3 = new Student();
    student3.setId(3);
    student3.setName("柳波");
    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("米老師");
    teacher1.setTitle("教授");
 
    Teacher teacher2 = new Teacher();
    teacher2.setId(2);
    teacher2.setName("丁老師");
    teacher2.setTitle("講師");
    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);
      }
    }
 
  }
}

輸出結果:

?

      1
      2
      3
      4
      5
      6
      {"students":[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:48:19 PM"},{"id":2,"name":"曹貴生","birthDay":"Jun 22, 2012 9:48:19 PM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 9:48:19 PM"}],"teachers":[{"id":1,"name":"米老師","title":"教授"},{"id":2,"name":"丁老師","title":"講師"}]}
      ----------------------------------
      key:students values:[{id=1.0, name=李坤, birthDay=Jun 22, 2012 9:48:19 PM}, {id=2.0, name=曹貴生, birthDay=Jun 22, 2012 9:48:19 PM}, {id=3.0, name=柳波, birthDay=Jun 22, 2012 9:48:19 PM}]
      [{id=1.0, name=李坤, birthDay=Jun 22, 2012 9:48:19 PM}, {id=2.0, name=曹貴生, birthDay=Jun 22, 2012 9:48:19 PM}, {id=3.0, name=柳波, birthDay=Jun 22, 2012 9:48:19 PM}]
      key:teachers values:[{id=1.0, name=米老師, id="codetool">

      延伸 · 閱讀

      精彩推薦
      主站蜘蛛池模板: 日韩不卡一区二区三区 | 九九大香尹人视频免费 | 成人一区二区丝袜美腿 | 欧美精品超清在线播放 | 精品国产影院 | 99久久精品国产片久人 | 成人精品一级毛片 | 国产一级在线免费观看 | 美女跪式抽搐gif动态图 | 香蕉视频在线观看网站 | 亚洲一级视频在线观看 | 国产资源免费 | 亚洲精品精品一区 | 亚洲一区二区成人 | 欧美色在线| 亚洲大爷操 | 国产婷婷成人久久av免费高清 | 九九精品99久久久香蕉 | 水蜜桃一二二区视在线 | 99re精品在线| 操岳母逼小说 | 国产福利在线观看永久视频 | 久久久无码精品无码国产人妻丝瓜 | 亚洲欧美日韩国产精品一区 | 亚洲一区二区三区深夜天堂 | 水蜜臀| 亚洲天堂色图 | 色中文字幕 | 精品在线网站 | 日本人与黑人做爰视频网站 | 亚洲精品久久碰 | 九九九九九九 | 国产啪精品视频网给免丝袜 | 久久99亚洲热最新地址获取 | 精品国产欧美一区二区五十路 | 故意短裙公车被强好爽在线播放 | 维修工的调教 | 513热点网深夜影院影院诶 | 国产99视频精品免费视频免里 | 高清在线观看免费 | 玩乳h文奶水和尚 |