//对象转Json
String userString = JSONObject.toJSONString(user);
//Json转对象
User parseObject = JSONObject.parseObject(userString,User.class);
代码
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
public class Test {
public static void main(String[] args) {
JSONObject JSONObject = new JSONObject();
User user = new User(1,"张三",18);
JSONObject.put("user", user);
System.out.println(JSONObject.get("user"));
//对象转Json
String userString = JSONObject.toJSONString(user);
System.out.println(userString);
//Json转对象
User parseObject = JSONObject.parseObject(userString,User.class);
System.out.println(parseObject);
}
}