共计 3629 个字符,预计需要花费 10 分钟才能阅读完成。
简介
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.
示例
本文主要涉及如下内容:
1、JSON 字符串转对象;
2、JSON 字符串转集合;
3、对象转 JSON 字符串;
App.java
package demo.gson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import demo.gson.entity.User;
import java.util.ArrayList;
import java.util.List;
public class App {public static void main(String[] args) {App.testConvertObjectToJson();
App.testConvertListToJson();
App.testConvertJsonToObject();
App.testConvertJsonToList();}
private static void testConvertObjectToJson() {System.out.println("== testConvertObjectToJson ==");
User user = new User("1", "0001", "程序员", "111", "test@qq.com");
System.out.println(new Gson().toJson(user));
// 结果:{"id":"1","usercode":"0001","username":"程序员","password":"111","email":"test@qq.com"}
}
private static void testConvertListToJson() {System.out.println("== testConvertListToJson ==");
List<User> list = new ArrayList<>();
list.add(new User("1", "0001", "程序员 1", "111", "test1@qq.com"));
list.add(new User("2", "0002", "程序员 2", "222", "test2@qq.com"));
System.out.println(new Gson().toJson(list));
// 结果:[{"id":"1","usercode":"0001","username":"程序员 1","password":"111","email":"test1@qq.com"},{"id":"2","usercode":"0002","username":"程序员 2","password":"222","email":"test2@qq.com"}]
}
private static void testConvertJsonToObject() {System.out.println("== testConvertJsonToObject ==");
String json = "{\"id\":\"1\",\"usercode\":\"0001\",\"username\":\" 程序员 \",\"password\":\"111\",\"email\":\"test@qq.com\"}";
System.out.println(new Gson().fromJson(json, User.class));
/*
结果:User[id='1', usercode='0001', username='程序员', password='111', email='test@qq.com', createTime=null]
*/
}
private static void testConvertJsonToList() {System.out.println("== testConvertJsonToList ==");
String json = "[{\"id\":\"1\",\"usercode\":\"0001\",\"username\":\" 程序员 1\",\"password\":\"111\",\"email\":\"test1@qq.com\"},{\"id\":\"2\",\"usercode\":\"0002\",\"username\":\" 程序员 2\",\"password\":\"222\",\"email\":\"test2@qq.com\"}]";
List<User> list = new Gson().fromJson(json, new TypeToken<List<User>>(){}.getType());
list.forEach(user -> System.out.println(user));
/*
结果:User[id='1', usercode='0001', username='程序员 1', password='111', email='test1@qq.com', createTime=null]
User[id='2', usercode='0002', username='程序员 2', password='222', email='test2@qq.com', createTime=null]
*/
}
}
User.java
package demo.gson.entity;
import java.util.Date;
import java.util.StringJoiner;
/**
* @Description: 用户
*/
public class User {
/**
* 主键
*/
private String id;
/**
* 用户编码
*/
private String usercode;
/**
* 用户名称
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 邮箱
*/
private String email;
/**
* 创建时间
*/
private Date createTime;
public User() {}
public User(String id, String usercode, String username, String password, String email) {
this.id = id;
this.usercode = usercode;
this.username = username;
this.password = password;
this.email = email;
}
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public String getUsercode() {return usercode;}
public void setUsercode(String usercode) {this.usercode = usercode;}
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 String getEmail() {return email;}
public void setEmail(String email) {this.email = email;}
public Date getCreateTime() {return createTime;}
public void setCreateTime(Date createTime) {this.createTime = createTime;}
@Override
public String toString() {return new StringJoiner(",", User.class.getSimpleName() + "[", "]")
.add("id='" + id + "'")
.add("usercode='" + usercode + "'")
.add("username='" + username + "'")
.add("password='" + password + "'")
.add("email='" + email + "'")
.add("createTime=" + createTime)
.toString();}
}
链接
- Gson 官网
正文完
发表至: java
2019-11-13