User:
package?com.naihe;
public?class?User?{
????private?String?name;
????private?int?age;
????public?User()?{}
????
????public?User(String?name,?int?age)?{
????????this.name?=?name;
????????this.age?=?age;
????}
????public?String?getName()?{
????????return?name;
????}
????public?void?setName(String?name)?{
????????this.name?=?name;
????}
????public?int?getAge()?{
????????return?age;
????}
????public?void?setAge(int?age)?{
????????this.age?=?age;
????}
}
Demo:
package?com.naihe;
import?com.alibaba.fastjson.JSONObject;
import?com.alibaba.fastjson.serializer.SerializerFeature;
public?class?FS?{
????public?static?void?main(String[]?args)?{
????????User?user1?=?new?User("小李",10);
????????String?JsStr1=?JSONObject.toJSONString(user1);
????????System.out.println(JsStr1);
????????User?user2?=?new?User("大李",100);
????????String?JsStr2=?JSONObject.toJSONString(user2,?SerializerFeature.WriteClassName);
????????System.out.println(JsStr2);
????????String?str?=?"{\"@type\":\"com.naihe.User\",\"age\":1000,\"name\":\"老李\"}";
????????Object?obj1?=?JSONObject.parse(str);
????????System.out.println(obj1);
????????Object?obj2?=?JSONObject.parseObject(str);
????????System.out.println(obj2);
????}
}
?