t01_json_str

六个小豹發表於2024-03-11

JSONUtil工具類

JSONUtil.toJsonStr:轉換為json字串

JSONUtil.toBean(strUser,User.class):json字串轉換為User

@Data
public class User {
    private String username;
    private String password;
}
public static void main(String[] args) {
    User user=new User();
    user.setPassword("abcdefg");
    user.setUsername("123456");
    System.out.println(user);
}
輸出:
User(username=123456, password=abcdefg)
// 轉換為json字串
String strUser= JSONUtil.toJsonStr(user);
System.out.println(strUser);
// json字串轉換為User
‘