使用GSON库将Java中的map键值对应结构对象转换为JSON

 更新时间:2016年06月22日 10:40:56   作者:李坤   我要评论

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的定义.

实体类:

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 + "]"; 
  } 
 
} 

测试类:

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

结果:

[[{"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

实体类:

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 + ", title=" + title 
        + "]"; 
  } 
 
} 

测试类:

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

输出结果:

{"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=米老师, title=教授}, {id=2.0, name=丁老师, title=讲师}] 
[{id=1.0, name=米老师, title=教授}, {id=2.0, name=丁老师, title=讲师}] 

相关文章

  • 使用java自带des加密算法实现文件加密和字符串加密

    使用java自带des加密算法实现文件加密和字符串加密

    这篇文章主要介绍了使用java自带des加密算法实现文件加密和字符串加密的示例,需要的朋友可以参考下
    2014-03-03
  • SpringCloud之微服务容错的实现

    SpringCloud之微服务容错的实现

    这篇文章主要介绍了SpringCloud之微服务容错的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Java中的静态内部类详解及代码示例

    Java中的静态内部类详解及代码示例

    这篇文章主要介绍了Java中的静态内部类详解及代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-10-10
  • 基于Java中的数值和集合详解

    基于Java中的数值和集合详解

    下面小编就为大家带来一篇基于Java中的数值和集合详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • 如何从官网下载Hibernate jar包的方法示例

    如何从官网下载Hibernate jar包的方法示例

    这篇文章主要介绍了如何从官网下载Hibernate jar包的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-04-04
  • 简单分析java中CMS回收器

    简单分析java中CMS回收器

    在本篇文章里我们给大家分享了关于java中CMS回收器的相关知识点内容,有需要的朋友们可以跟着学习下。
    2018-10-10
  • Action访问Servlet的API的简单实例

    Action访问Servlet的API的简单实例

    下面小编就为大家带来一篇Action访问Servlet的API的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • java 中线程等待与通知的实现

    java 中线程等待与通知的实现

    这篇文章主要介绍了java 中线程等待与通知的实现的相关资料,希望通过本文能帮助到大家,让大家掌握这样的功能,需要的朋友可以参考下
    2017-09-09
  • java ThreadLocal使用案例详解

    java ThreadLocal使用案例详解

    这篇文章主要为大家详细介绍了java ThreadLocal的使用案例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • spring boot实战教程之shiro session过期时间详解

    spring boot实战教程之shiro session过期时间详解

    这篇文章主要给大家介绍了关于spring boot实战教程之shiro session过期时间的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-10-10

最新评论