代码Demo:
package com.eszb;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eszb.core.entity.Admin;
/**
* @Title : TestJSONConvertToObject
* @Author :MrYu
* @Description :&{ TestJSONConvertToObject.java 2019/6/26 on 20:36 version 1.0 }
*/
public class TestJSONConvertToObject {
public static void main(String[] args) {
Admin admin = new Admin();
admin.setId(1);
admin.setRealName("张三");
//将对象转为JSONObject
JSONObject adminJson= (JSONObject) JSON.toJSON(admin);
System.out.println("admin JSONObject:"+adminJson);
//将JSONString转成对象
Admin admin1 = JSONObject.parseObject(adminJson.toJSONString(), Admin.class);
System.out.println("admin Java Object:"+admin1);
}
}
执行结果