依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
User对象
public class User {
private Integer id;
private String username;
private String password;
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User对象toString{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
fastjson解析
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FastJsonTest {
/*基本数据类型
[
"aaa",
"bbb"
]
*/
public void primitiveTypeJson() {
List<String> stringList = new ArrayList<String>();
stringList.add("aaa");
stringList.add("bbb");
System.out.println("-----------------基本数据类型转json-----------------");
String stringListToJson = JSON.toJSONString(stringList);
System.out.println(stringListToJson);//["aaa","bbb"]
JSONArray jsonArray = JSON.parseArray(stringListToJson);
for (int i = 0; i < jsonArray.size(); i++) {
System.out.println(jsonArray.getString(i));//aaa/bbb
}
System.out.println("-----------------基本数据类型json转对象-----------------");
System.out.println("方式一:解析成String对象再放入List");
List<String> jsonToStringList = new ArrayList<String>();
JSONArray jsonArray2 = JSON.parseArray(stringListToJson);
for (int i = 0; i < jsonArray2.size(); i++) {
jsonToStringList.add(jsonArray.getString(i));
}
//aaa
//bbb
for (String str : jsonToStringList) {
System.out.println(str);
}
System.out.println("方式二:直接解析为List<String>");
List<String> jsonToStringList2 = JSON.parseArray(stringListToJson, String.class);
//aaa
//bbb
for (String str : jsonToStringList2) {
System.out.println(str);
}
}
/*简单对象json操作
{
"id": 1,
"username": "username1",
"password": "password1"
}
*/
public void userJson() {
System.out.println("-----------------简单对象转json-----------------");
User user = new User(1, "username1", "password1");
String userToJson = JSON.toJSONString(user);
System.out.println(userToJson);//{"id":1,"username":"username1","password":"password1"}
System.out.println("-----------------解析简单对象的json-----------------");
JSONObject jsonObject = JSON.parseObject(userToJson);
System.out.println(jsonObject.getInteger("id"));//1
System.out.println(jsonObject.getString("username"));//username1
System.out.println(jsonObject.getString("password"));//password1
System.out.println("-----------------简单对象的json转对象-----------------");
JSONObject jsonObject2 = JSON.parseObject(userToJson);
User user1 = jsonObject2.toJavaObject(User.class);
System.out.println(user1);//User对象toString{id=1, username='username1', password='password1'}
}
/*简单对象数组和List
[
{
"id": 1,
"username": "username1",
"password": "password1"
},
{
"id": 2,
"username": "username2",
"password": "password2"
}
]
*/
public void userListJson() {
User user1 = new User(1, "username1", "password1");
User user2 = new User(2, "username2", "password2");
List<User> userList = new ArrayList<User>();
userList.add(user1);
userList.add(user2);
User[] userArr = new User[2];
userArr[0] = user1;
userArr[1] = user2;
System.out.println("-----------------List<User>转json-----------------");
String userListToJson = JSON.toJSONString(userList);
//[{"id":1,"username":"username1","password":"password1"},{"id":2,"username":"username2","password":"password2"}]
System.out.println(userListToJson);
System.out.println("-----------------user数组转json-----------------");
String userArrToJson = JSON.toJSONString(userArr);
//[{"id":1,"username":"username1","password":"password1"},{"id":2,"username":"username2","password":"password2"}]
System.out.println(userArrToJson);
System.out.println("List<User>转json和user数组转json是一样的");
System.out.println("-----------------解析List<User>的json/user数组的json-----------------");
JSONArray jsonArray = JSON.parseArray(userListToJson);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.getInteger("id"));//1/2
System.out.println(jsonObject.getString("username"));//username1/username12
System.out.println(jsonObject.getString("password"));//password1/password2
}
System.out.println("-----------------List<User>的json/user数组的json转对象-----------------");
System.out.println("方式一:解析一个User再放入List");
List<User> jsonToUserList = new ArrayList<User>();
JSONArray jsonArray2 = JSON.parseArray(userListToJson);
for (int i = 0; i < jsonArray2.size(); i++) {
//jsonElement转对象方式一
JSONObject jsonObject = jsonArray2.getJSONObject(i);
User jsonToUser = jsonObject.toJavaObject(User.class);
jsonToUserList.add(jsonToUser);
}
//User对象toString{id=1, username='username1', password='password1'}
//User对象toString{id=2, username='username2', password='password2'}
for (User user : jsonToUserList) {
System.out.println(user);
}
System.out.println("方式二:直接解析为List<User>");
List<User> jsonToUserList2 = JSON.parseArray(userListToJson, User.class);
//User对象toString{id=1, username='username1', password='password1'}
//User对象toString{id=2, username='username2', password='password2'}
for (User user : jsonToUserList2) {
System.out.println(user);
}
}
/* 复杂对象
{
"Integer": 1,
"userList": [
{
"id": 1,
"username": "username1",
"password": "password1"
},
{
"id": 2,
"username": "username2",
"password": "password2"
}
],
"String": "String",
"Double": 1.23,
"user": {
"id": 1,
"username": "username1",
"password": "password1"
}
}
*/
public void complexGsonTest() {
User user1 = new User(1, "username1", "password1");
User user2 = new User(2, "username2", "password2");
List<User> userList = new ArrayList<User>();
userList.add(user1);
userList.add(user2);
Map<String, Object> map = new HashMap<String, Object>();
map.put("String", "String");//字符串
map.put("Integer", 1);//整数
map.put("Double", 1.23);//浮点数
map.put("user", user1);//对象
map.put("userList", userList);//对象List
String complexJson = JSON.toJSONString(map);
//{"Integer":1,"userList":[{"id":1,"username":"username1","password":"password1"},{"id":2,"username":"username2","password":"password2"}],"String":"String","Double":1.23,"user":{"id":1,"username":"username1","password":"password1"}}
System.out.println(complexJson);
System.out.println("-----------------解析复杂对象的json-----------------");
JSONObject jsonObject = JSON.parseObject(complexJson);
// 字符串
System.out.println(jsonObject.getString("String"));
// 整数
System.out.println(jsonObject.getInteger("Integer"));
// 浮点数
System.out.println(jsonObject.getDouble("Double"));
// User:查看userJson函数具体解析
JSONObject userJsonObject = jsonObject.getJSONObject("user");
System.out.println(userJsonObject.getInteger("id"));
System.out.println(userJsonObject.getString("username"));
System.out.println(userJsonObject.getString("password"));
// List<User>:查看userListJson函数具体解析
// 获得JsonArray
JSONArray userListJsonArray = jsonObject.getJSONArray("userList");
// 遍历JsonArray
for (int i = 0; i < userListJsonArray.size(); i++) {
JSONObject asJsonObject = userListJsonArray.getJSONObject(i);
System.out.println(asJsonObject.getInteger("id"));
System.out.println(asJsonObject.getString("username"));
System.out.println(asJsonObject.getString("password"));
}
System.out.println("-----------------解析复杂对象的json转成对象,基本数据类型同上-----------------");
System.out.println("User对象:查看userJson函数具体解析");
JSONObject userJsonObject2 = jsonObject.getJSONObject("user");
User jsonToUser = userJsonObject2.toJavaObject(User.class);
System.out.println(jsonToUser);
System.out.println("List<User>:查看userListJson函数具体解析");
JSONArray userListJsonArray2 = jsonObject.getJSONArray("userList");
List<User> users = JSON.parseArray(userListJsonArray2.toJSONString(), User.class);
for (User user : users) {
System.out.println(user);
}
}
/* 复杂对象转map再解析
{
"Integer": 1,
"userList": [
{
"id": 1,
"username": "username1",
"password": "password1"
},
{
"id": 2,
"username": "username2",
"password": "password2"
}
],
"integerList": [
1,
2
],
"stringList": [
"a",
"b"
],
"doubleList": [
1.23,
3.45
],
"String": "String",
"Double": 1.23,
"user": {
"id": 1,
"username": "username1",
"password": "password1"
}
}
*/
public void complexMapJson() {
User user1 = new User(1, "username1", "password1");
User user2 = new User(2, "username2", "password2");
List<User> userList = new ArrayList<User>();
userList.add(user1);
userList.add(user2);
List<Integer> integerList = new ArrayList<Integer>();
integerList.add(1);
integerList.add(2);
List<String> stringList = new ArrayList<String>();
stringList.add("a");
stringList.add("b");
List<Double> doubleList = new ArrayList<Double>();
doubleList.add(1.23);
doubleList.add(3.45);
Map<String, Object> map = new HashMap<String, Object>();
map.put("String", "String");//字符串
map.put("Integer", 1);//整数
map.put("Double", 1.23);//浮点数
map.put("user", user1);//对象
map.put("integerList", integerList);//integerList
map.put("stringList", stringList);//stringList
map.put("doubleList", doubleList);//doubleList
map.put("userList", userList);//对象List
// map转json
String complexMapJson = JSON.toJSONString(map);
System.out.println(complexMapJson);
//{"Integer":1,"userList":[{"id":1,"username":"username1","password":"password1"},{"id":2,"username":"username2","password":"password2"}],"integerList":[1,2],"stringList":["a","b"],"doubleList":[1.23,3.45],"String":"String","Double":1.23,"user":{"id":1,"username":"username1","password":"password1"}}
// https://www.cnblogs.com/heqiyoujing/p/9432153.html
// json转Map
Map jsonToMap = JSON.parseObject(complexMapJson, Map.class);
System.out.println("-----------------解析String-----------------");
System.out.println(jsonToMap.get("String")); //String
System.out.println("-----------------解析Integer-----------------");
System.out.println(jsonToMap.get("Integer")); //1.0
System.out.println("-----------------解析Double-----------------");
System.out.println(jsonToMap.get("Double")); //1.23
System.out.println("-----------------解析User-----------------");
String userJson = jsonToMap.get("user").toString();
System.out.println(userJson); //{id=1.0, username=username1, password=password1}
User jsonToUser2 = JSON.parseObject(userJson, User.class);
System.out.println(jsonToUser2); //User对象toString{id=1, username='username1', password='password1'}
System.out.println("-----------------解析IntegerList-----------------");
System.out.println("方式一:直接从map中获取");
List<Integer> jsonToIntegerList = (List<Integer>) jsonToMap.get("integerList");
System.out.println("方式二:先获得对应json再解析为IntegerList,具体看-基本数据类型");
String integerListJson = jsonToMap.get("integerList").toString();
System.out.println(integerListJson); //[1.0, 2.0]
List<Integer> jsonToIntegerList2 = JSON.parseArray(integerListJson, Integer.class);
//方式一:1.0 方式二:1
//方式一:2.0 方式二:2
for (int i = 0; i < jsonToIntegerList.size(); i++) {
System.out.println("方式一:" + jsonToIntegerList.get(i) + " 方式二:" + jsonToIntegerList2.get(i));
}
System.out.println("-----------------解析StringList-----------------");
System.out.println("方式一:直接从map中获取");
List<String> jsonToStringList = (List<String>) jsonToMap.get("stringList");
System.out.println("方式二:先获得对应json再解析为StringList,具体看-基本数据类型");
String stringListJson = jsonToMap.get("stringList").toString();
System.out.println(stringListJson); //[a, b]
List<String> jsonToStringList2 = JSON.parseArray(stringListJson, String.class);
//方式一:a 方式二:a
//方式一:b 方式二:b
for (int i = 0; i < jsonToStringList.size(); i++) {
System.out.println("方式一:" + jsonToStringList.get(i) + " 方式二:" + jsonToStringList2.get(i));
}
System.out.println("-----------------解析DoubleList-----------------");
System.out.println("方式一:直接从map中获取");
List<Double> jsonToDoubleList = (List<Double>) jsonToMap.get("doubleList");
System.out.println("方式二:先获得对应json再解析为DoubleList,具体看-基本数据类型");
String doubleListJson = jsonToMap.get("doubleList").toString();
System.out.println(doubleListJson); //[1.23, 3.45]
List<Double> jsonToDoubleList2 = JSON.parseArray(doubleListJson, Double.class);
//方式一:1.23 方式二:1.23
//方式一:3.45 方式二:3.45
for (int i = 0; i < jsonToStringList.size(); i++) {
System.out.println("方式一:" + jsonToDoubleList.get(i) + " 方式二:" + jsonToDoubleList2.get(i));
}
System.out.println("-----------------解析UserList-----------------");
System.out.println("对像类型List不能直接从map中获取");
String userListJson = jsonToMap.get("userList").toString();
// 先获得对应json再解析为DoubleList,具体看-简单对象数组和List
System.out.println(userListJson); //[{id=1.0, username=username1, password=password1}, {id=2.0, username=username2, password=password2}]
List<User> jsonToUserList = JSON.parseArray(userListJson, User.class);
//User对象toString{id=1, username='username1', password='password1'}
//User对象toString{id=2, username='username2', password='password2'}
for (User u : jsonToUserList) {
System.out.println(u);
}
}
public static void main(String[] args) {
FastJsonTest fastJsonTest = new FastJsonTest();
fastJsonTest.complexGsonTest();
}
}