欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-12 22:08:02
map的存储结构式key/value形式,key 和 value可以是普通类型,也可以是自己写的javabean(本文),还可以是带有泛型的list. (gson的git...

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=讲师}]