Java Gson 使用,Gson将字符串转为list

Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。

对象转为字符串

String ps =gson.toJson(person);

字符串转为对象

Person person =gson.fromJson(json, Person.class);

字符串为为list

List<Person> persons =gson.fromJson(json, new TypeToken<List<Person>>() {}.getType());


对象转为字符串,以键值对的方式返回字符串。

字符串转为对象,要注意字符串的格式。格式与对象转换的字符串一样,可以参照。

注意:

需要jar包 gson-2.3.jar,可从gson的官方下载所需版本。

字符串转为对象时,""字符串转出来对象为null,"{}" 转出来有对象,但各个字段为空值。

对象内有list字段时,json字符串的写法

json = "{\"id\":2,\"name\":\"abc\",\"books\":[\"xiqu\",\"wenzhang\",\"xiaoshuo\"]}";

json为list的写法

json = "[{\"id\":2,\"name\":\"abc\",\"books\":[\"xiqu\",\"wenzhang\",\"xiaoshuo\"]},"
+ "{\"id\":3,\"name\":\"def\",\"books\":[\"dianshi\",\"wenzhang\",\"xiaoshuo\"]}]";


代码示例:

[java]  view plain  copy
  1. import java.util.LinkedList;  
  2. import java.util.List;  
  3.   
  4. import com.google.gson.Gson;  
  5. import com.google.gson.reflect.TypeToken;  
  6.   
  7. class Person {  
  8.     private long id;  
  9.     private String name;  
  10.     private List<String> books;  
  11.   
  12.     public Person(long id, String name) {  
  13.         this.id = id;  
  14.         this.name = name;  
  15.     }  
  16.   
  17.     public Person(long id, String name, List<String> books) {  
  18.         this(id, name);  
  19.         this.books = books;  
  20.     }  
  21.   
  22.     public long getId() {  
  23.         return id;  
  24.     }  
  25.   
  26.     public void setId(long id) {  
  27.         this.id = id;  
  28.     }  
  29.   
  30.     public String getName() {  
  31.         return name;  
  32.     }  
  33.   
  34.     public void setName(String name) {  
  35.         this.name = name;  
  36.     }  
  37.   
  38.     public List<String> getBooks() {  
  39.         return books;  
  40.     }  
  41.   
  42.     public void setBooks(List<String> books) {  
  43.         this.books = books;  
  44.     }  
  45.   
  46.     public String toString(){  
  47.         return "Person : { id=" + this.id + "  name=" + this.name + "  books=" + this.books + " }";  
  48.     }  
  49. }  
  50.   
  51. public class Gsontest {  
  52.     static Gson gson = new Gson();  
  53.   
  54.     public static void main(String[] args) {  
  55.         toJson();  
  56.         fromJson();  
  57.     }  
  58.   
  59.     static void toJson() {  
  60.         List<String> books = new LinkedList<String>();  
  61.         books.add("abook");  
  62.         books.add("bbook");  
  63.         books.add("cbook");  
  64.         Person person = new Person(1"xiao", books);  
  65.   
  66.         // 将一个对象转为以键值对表示的字符串,对象内可包含list等都可以  
  67.         String ps = gson.toJson(person);  
  68.         System.out.println(ps);  
  69.   
  70.         // 将一个对象List 转为以键值对表示的字符串  
  71.         List<Person> persons = new LinkedList<Person>();  
  72.         persons.add(person);  
  73.         persons.add(new Person(2"ming", books));  
  74.         System.out.println(gson.toJson(persons));  
  75.     }  
  76.   
  77.     static void fromJson() {  
  78.         // 空字符串转出来对象为null  
  79.         String json = "";  
  80.         // 提供两个参数,分别是json字符串以及需要转换对象的类型。  
  81.         Person person = gson.fromJson(json, Person.class);  
  82.         System.out.println(person);  
  83.   
  84.         // 注:字符串必须在{}内,list类型包含在[]内,否则转换失败抛异常  
  85.         json = "{}";  
  86.         // {}内无内容,可以转换成对象,对象不为空,但是各个字段没有值  
  87.         person = gson.fromJson(json, Person.class);  
  88.         System.out.println(person);  
  89.   
  90.         // 注意键值对的写法,如果转换出错,很可能是写法不对,写法可以参照由 toJson 转出来的字符串.  
  91.         json = "{\"id\":2,\"name\":\"abc\",\"books\":[\"xiqu\",\"wenzhang\",\"xiaoshuo\"]}";  
  92.         person = gson.fromJson(json, Person.class);  
  93.         System.out.println(person);  
  94.   
  95.         // TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换  
  96.         json = "[{\"id\":2,\"name\":\"abc\",\"books\":[\"xiqu\",\"wenzhang\",\"xiaoshuo\"]},"  
  97.                 + "{\"id\":3,\"name\":\"def\",\"books\":[\"dianshi\",\"wenzhang\",\"xiaoshuo\"]}]";  
  98.         List<Person> persons = gson.fromJson(json, new TypeToken<List<Person>>() {  
  99.         }.getType());  
  100.         System.out.println(persons);  
  101.     }  
  102. }  

猜你喜欢

转载自blog.csdn.net/yapengliu/article/details/79711400