fastjson简单使用

发布时间:2023年12月27日

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);

????}
}

?

文章来源:https://blog.csdn.net/2301_80520893/article/details/135197656
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。