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

将Java的List结构通过GSON库转换为JSON的方法示例

程序员文章站 2024-03-12 16:28:38
发现了google的gson,因为之前对于protocolbuf有一些了解,带着一些好奇心,我开始使用了gson。 github主页:https://github.com...

发现了google的gson,因为之前对于protocolbuf有一些了解,带着一些好奇心,我开始使用了gson。
github主页:https://github.com/google/gson
经过比较,gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需要序列化的字段。
将list或者map转换成json非常简单:    

public string getjsondata(list<?> list) {
  gson gson = new gson();
  string jsonstring = gson.tojson(list);
  return jsonstring;
}

下面我们会有详细的例子。

示例
简单对象转化和带泛型的list转化:

实体类:

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

测试类:

import java.util.arraylist; 
import java.util.date; 
import java.util.list; 
 
import com.google.gson.gson; 
import com.google.gson.reflect.typetoken; 
 
public class gsontest1 { 
 
  public static void main(string[] args) { 
    gson gson = new gson(); 
 
    student student1 = new student(); 
    student1.setid(1); 
    student1.setname("李坤"); 
    student1.setbirthday(new date()); 
 
    // ////////////////////////////////////////////////////////// 
    system.out.println("----------简单对象之间的转化-------------"); 
    // 简单的bean转为json 
    string s1 = gson.tojson(student1); 
    system.out.println("简单bean转化为json===" + s1); 
 
    // json转为简单bean 
    student student = gson.fromjson(s1, student.class); 
    system.out.println("json转为简单bean===" + student); 
    // 结果: 
    // 简单bean转化为json==={"id":1,"name":"李坤","birthday":"jun 22, 2012 8:27:52 am"} 
    // json转为简单bean===student [birthday=fri jun 22 08:27:52 cst 2012, id=1, 
    // name=李坤] 
    // ////////////////////////////////////////////////////////// 
 
    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> list = new arraylist<student>(); 
    list.add(student1); 
    list.add(student2); 
    list.add(student3); 
 
    system.out.println("----------带泛型的list之间的转化-------------"); 
    // 带泛型的list转化为json 
    string s2 = gson.tojson(list); 
    system.out.println("带泛型的list转化为json==" + s2); 
 
    // json转为带泛型的list 
    list<student> retlist = gson.fromjson(s2, 
        new typetoken<list<student>>() { 
        }.gettype()); 
    for (student stu : retlist) { 
      system.out.println(stu); 
    } 
 
    // 结果: 
    // 带泛型的list转化为json==[{"id":1,"name":"李坤","birthday":"jun 22, 2012 8:28:52 am"},{"id":2,"name":"曹贵生","birthday":"jun 22, 2012 8:28:52 am"},{"id":3,"name":"柳波","birthday":"jun 22, 2012 8:28:52 am"}] 
    // student [birthday=fri jun 22 08:28:52 cst 2012, id=1, name=李坤] 
    // student [birthday=fri jun 22 08:28:52 cst 2012, id=2, name=曹贵生] 
    // student [birthday=fri jun 22 08:28:52 cst 2012, id=3, name=柳波] 
 
  } 
} 

执行结果:

----------简单对象之间的转化------------- 
简单bean转化为json==={"id":1,"name":"李坤","birthday":"jun 22, 2012 9:10:31 pm"} 
json转为简单bean===student [birthday=fri jun 22 21:10:31 cst 2012, id=1, name=李坤] 
----------带泛型的list之间的转化------------- 
带泛型的list转化为json==[{"id":1,"name":"李坤","birthday":"jun 22, 2012 9:10:31 pm"},{"id":2,"name":"曹贵生","birthday":"jun 22, 2012 9:10:31 pm"},{"id":3,"name":"柳波","birthday":"jun 22, 2012 9:10:31 pm"}] 
student [birthday=fri jun 22 21:10:31 cst 2012, id=1, name=李坤] 
student [birthday=fri jun 22 21:10:31 cst 2012, id=2, name=曹贵生] 
student [birthday=fri jun 22 21:10:31 cst 2012, id=3, name=柳波]