昨天讀一篇文章,看到gson這個詞,一開始還覺得做者寫錯了,問了度娘以後才發現是我才疏學淺,因而大概瞭解了一下gson用法,整體來講仍是很簡單的。java
Gson.jar下載json
JavaBean轉jsonmarkdown
//javaBean-->json
@Test
public void test1(){
Gson gson = new Gson();
Person p = new Person("張三", "123", 500, new Book("三國演義", "羅貫中"));
String json = gson.toJson(p);
System.out.println(json);
}
json轉javabeaneclipse
//json-->javaBean
@Test
public void test2(){
String json = "{\"username\":\"張三\",\"password\":\"123\",\"money\":500,\"book\":{\"id\":0,\"name\":\"三國演義\",\"price\":0,\"author\":\"羅貫中\"}}";
Gson gson = new Gson();
Person p = gson.fromJson(json, Person.class);
System.out.println(p.getUsername()+"--"+p.getBook().getName());
}
List集合轉jsonui
//list-->json
@Test
public void test3() {
List<Person> list = new ArrayList<Person>();
Person p = new Person("張三", "123", 500, new Book("三國演義", "羅貫中"));
list.add(p);
p = new Person("李四", "123", 500, new Book("三國演義", "羅貫中"));
list.add(p);
Gson gson = new Gson();
String gsonStr = gson.toJson(list);
System.out.println(gsonStr);
}
Map集合轉jsonthis
//map-->json
@Test
public void test4() {
Map<String, Person> map = new HashMap<String, Person>();
Person p = new Person("張三", "123", 500, new Book("三國演義", "羅貫中"));
map.put("p1", p);
p = new Person("李四", "123", 500, new Book("三國演義", "羅貫中"));
map.put("p2", p);
Gson gson = new Gson();
String gsonStr = gson.toJson(map);
System.out.println(gsonStr);
}
json字符串轉List集合spa
//json-->list
@Test
public void test5() {
String str = "[{\"username\":\"張三\",\"password\":\"123\",\"money\":500,\"book\":{\"id\":0,\"name\":\"三國演義\",\"price\":0,\"author\":\"羅貫中\"}},{\"username\":\"李四\",\"password\":\"123\",\"money\":500,\"book\":{\"id\":0,\"name\":\"三國演義\",\"price\":0,\"author\":\"羅貫中\"}}]";
Gson gson = new Gson();
List<Person> ps = gson.fromJson(str,new TypeToken<List<Person>>() {
}.getType());
for (Person person : ps) {
System.out.println(person.getUsername()+"---"+person.getBook().getName());
}
}
json字符串轉Map集合.net
//json-->map
@Test
public void test6(){
String str = "{\"p1\":{\"username\":\"張三\",\"password\":\"123\",\"money\":500,\"book\":{\"id\":0,\"name\":\"三國演義\",\"price\":0,\"author\":\"羅貫中\"}},\"p2\":{\"username\":\"李四\",\"password\":\"123\",\"money\":500,\"book\":{\"id\":0,\"name\":\"紅樓夢\",\"price\":0,\"author\":\"曹雪芹\"}}}";
Gson gson = new Gson();
Map<String, Person> map = gson.fromJson(str, new TypeToken<Map<String, Person>>(){}.getType());
Set<String> keys = map.keySet();
for(String key:keys){
System.out.println(map.get(key).getUsername()+"--"+map.get(key).getBook().getName());
}
}
相關的兩個javabean
Person.javacode
public class Person {
private String username;
private String password;
private int money;
private Book book;
public Person() {
}
public Person(Book book) {
this.book = book;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Person(String username, String password, int money, Book book) {
super();
this.username = username;
this.password = password;
this.money = money;
this.book = book;
}
}
Book.javaip
public class Book {
private int id;
private String name;
private int price;
private String author;
private Detail detail;
private Attribute attribute;
public Attribute getAttribute() {
return attribute;
}
public void setAttribute(Attribute attribute) {
this.attribute = attribute;
}
public Detail getDetail() {
return detail;
}
public void setDetail(Detail detail) {
this.detail = detail;
}
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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Book(String name, String author) {
this.name = name;
this.author = author;
}
public Book() {
}
}
注意事項:
把json字符串轉爲集合時,new TypeToken<>(){}.getType()
在eclipse中貌似不可以自動提示,若是你的電腦也是這樣的話,建議這裏的代碼本身手動敲一下,eclipse只是不會自動提示,本身敲出來的是不會報錯的,嗯,我就遇到這一個小小的問題,整體來講這個東東仍是很好用的。