好多地方都用到了java對象,要想把java對象轉成json對象,不能直接轉java
好比,json
- public class User {
-
- private String username;
- private String password;
-
- 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;
- }
-
- }
咱們使用fastjson來轉化this
若是直接使用對象
JSONObject o = JSON.parseObject(user.toString());
這樣是不行,須要先轉成object,再由object轉成fastjsonget
Object one = JSON.toJSON(user);
System.out.println(one);
JSONObject jsonObject = JSON.parseObject(one.toString());
這樣就行了ast