在前端中可以使用JS来将JSON与字符串之间进行转化,如下示例:
//json对象
let student={name:"Jack",age:19,sex:"男"};
console.log(student);
//将json对象转换为字符串
let stringJson=JSON.stringify(student);
console.log(stringJson);
//将字符串转换为json对象
console.log(JSON.parse(stringJson));
使用json字符串在前后端进行数据的传输,可以降低交互成本,于是要求在服务端也可以对json进行灵活的处理
一. FastJson方式
jar包下载地址:https://pan.baidu.com/s/1CkQmeMnwRNrbnmaTenXJlA
示例代码:
先建立两个实体类,之间为包含关系:
User.java:
package com.qianqian.practice.entity;
public class User {
private int id;
private String name;
public User() {
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
}
Admin.java:
package com.qianqian.practice.entity;
import java.util.List;
public class Admin {
private String name;
private List<User> userList;
public Admin() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
@Override
public String toString() {
return "Admin{" +
"name='" + name + '\'' +
", userList=" + userList +
'}';
}
public Admin(String name, List<User> userList) {
this.name = name;
this.userList = userList;
}
}
然后新建测试类,构建对象,并做字符串和JSON对象的转化
package com.qianqian.practice.test;
import com.alibaba.fastjson.JSON;
import com.qianqian.practice.entity.Admin;
import com.qianqian.practice.entity.User;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Admin admin = Test.getAdminInstance();
//将对象转换为JSON字符串
String string = JSON.toJSONString(admin);
System.out.println(string);
//将字符串再转化为对象
Admin admin1 = JSON.parseObject(string, Admin.class);
List<User> userList = admin1.getUserList();
for (User user : userList) System.out.println(user.toString());
}
public static Admin getAdminInstance() {
User user1 = new User(1, "lili");
User user2 = new User(2, "haha");
User user3 = new User(3, "baba");
ArrayList<User> userArrayList = new ArrayList<>();
userArrayList.add(user1);
userArrayList.add(user2);
userArrayList.add(user3);
Admin admin = new Admin("boss", userArrayList);
return admin;
}
}
运行结果:
二. Jackson方式
jar包下载地址:(三个全部需要导入):https://pan.baidu.com/s/1yCBvglM1hkF5OoHlbRJcaw
代码示例:
package com.qianqian.practice.test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qianqian.practice.entity.Admin;
import com.qianqian.practice.entity.User;
import java.io.IOException;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
Admin admin = Test.getAdminInstance();
ObjectMapper objectMapper = new ObjectMapper();
try {
//将dui对象转换为JSON字符串
String string = objectMapper.writeValueAsString(admin);
System.out.println(string);
//将JSON字符串转化为对象
Admin admin1 = objectMapper.readValue(string, Admin.class);
for (User user : admin1.getUserList()) System.out.println(user);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Admin getAdminInstance() {
User user1 = new User(1, "lili");
User user2 = new User(2, "haha");
User user3 = new User(3, "baba");
ArrayList<User> userArrayList = new ArrayList<>();
userArrayList.add(user1);
userArrayList.add(user2);
userArrayList.add(user3);
Admin admin = new Admin("boss", userArrayList);
return admin;
}
}
运行结果:
谢谢观看!希望赏个赞呗😋。
来源:CSDN
作者:秋.凝
链接:https://blog.csdn.net/qq_42013035/article/details/104124402