public class Person {
private String username;
private String password;
private double money;
private Book book;
public Person() {}
public Person(String username, String password, double money, Book book) {
this.username = username;
this.password = password;
this.money = money;
this.book = book;
}
public String toString() {
return "username: " + username + ", password: " + password + ", money: " + money + ", 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 double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
}
class Book {
private int id;
private String name;
private double price;
private String author;
public Book() {}
public Book(String name, String author) {
this.name = name;
this.author = author;
}
public Book(int id, String name, double price, String author) {
this.id = id;
this.name = name;
this.price = price;
this.author = author;
}
public String toString() {
return "id: " + id + ", name: " + name + ", price: " + price + ", author: " + author;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
public class ObjectToJson {
/**
* javabean to json
*
* @param person
* @return
*/
public static String javabeanToJson(Person person) {
Gson gson = new Gson();
String json = gson.toJson(person);
return json;
}
/**
* list to json
* @param list
* @return
*/
public static String listToJson(List<Person> list) {
Gson gson = new Gson();
String json = gson.toJson(list);
return json;
}
/**
* map to json
* @param map
* @return
*/
public static String mapToJson(Map<String, Person> map) {
Gson gson = new Gson();
String json = gson.toJson(map);
return json;
}
}
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class JsonToObject {
/**
* json to javabean
* @param json
*/
public static void jsonToJavaBean(String json) {
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);
System.out.println(person.toString());
}
/**
* json字符串转List集合
* @param json
*/
public static void jsonToList(String json) {
Gson gson = new Gson();
List<Person> persons = gson.fromJson(json, new TypeToken<List<Person>>() {}.getType());
for(Person person : persons) {
System.out.println(person);
}
}
public static void jsonToMap(String json) {
Gson gson = new Gson();
Map<String, Person> maps = gson.fromJson(json, new TypeToken<Map<String, Person>>() {}.getType());
for(Map.Entry<String, Person> entry : maps.entrySet()) {
System.out.println("key: " + entry.getKey() + " " + ", value: " + entry.getValue());
}
}
}
public class GsonDemo {
public static void main(String[] args) {
// javabean to json
Person person = new Person("彭晓", "dslkfjdslf", 344, new Book("java", "vincent"));
System.out.println(ObjectToJson.javabeanToJson(person));
// list to json
List<Person> list = new ArrayList<Person>();
list.add(person);
Person p = new Person("彭真", "slfjdj", 32423, new Book("android", "郭林"));
list.add(p);
System.out.println(ObjectToJson.listToJson(list));
// map json
Map<String, Person> map = new HashMap<>();
map.put("m1", person);
map.put("m2", p);
System.out.println(ObjectToJson.mapToJson(map));
// 下面是json直接通过gson转换为实例对象
String json = "{'username':'张三', 'password':'123', 'money':500, 'book':{'id':0, 'name':'三国演义', 'price':'0',"
+ "'author':'罗贯中'}}";
JsonToObject.jsonToJavaBean(json);
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':'罗贯中'}}]";
JsonToObject.jsonToList(str);
String mapStr = "{'m1':{'username':'张三','password':'123','money':500,'book':{'id':0, 'name':'三国演义', 'price':0,'author':'罗贯中'}},"
+ "'m2':{'username':'李四','password':'123','money':500,'book':{'id':0, 'name':'三国演义', 'price':0, 'author':'罗贯中'}}}";
JsonToObject.jsonToMap(mapStr);
}
}