当前位置:网站首页>@jsonfield annotation in fastjson
@jsonfield annotation in fastjson
2022-06-23 05:36:00 【y_ bccl27】
Use fastjson The following dependencies must be introduced before , The current version is 1.2.75
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>JSONField Medium name Property to specify JSON In a string key The name of
[email protected] Annotation works on attributes ( Member variables ) On
import com.alibaba.fastjson.annotation.JSONField;
public class Person {
@JSONField(name = "userName")
private String name;
@JSONField(name = "AGE")
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}Serialization test :
import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;
import java.util.Date;
public class Demo {
public static void main(String[] args){
Person person=new Person();
person.setName(" Zhang San ");
person.setAge("20");
person.setDate(new Date());
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}Execute the above code , The output is :
{"AGE":"20","userName":" Zhang San "}Deserialization test :
import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;
public class Demo {
public static void main(String[] args){
String jsonStr="{\"AGE\":\"20\",\"userName\":\" Zhang San \"}";
Person person = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Person.class);
System.out.println("json to bean:" + person.getName());
}
}Execute the above code , The output is :
json to bean: Zhang San @JSONField It works on Field when , Its name Not only does it define the name of the output , It also defines the input key The name of
[email protected] Annotation works on methods
import com.alibaba.fastjson.annotation.JSONField;
public class Person {
private String name;
private String age;
// For serialization operations
@JSONField(name = "userName")
public String getName() {
return name;
}
// For deserialization
@JSONField(name = "userName")
public void setName(String name) {
this.name = name;
}
@JSONField(name = "AGE")
public String getAge() {
return age;
}
@JSONField(name = "AGE")
public void setAge(String age) {
this.age = age;
}
}import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;
public class Demo {
public static void main(String[] args){
// serialize
Person person=new Person();
person.setName(" Zhang San ");
person.setAge("20");
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
// Deserialization
// String jsonStr="{\"AGE\":\"20\",\"userName\":\" Zhang San \"}";
// Person person = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Person.class);
// System.out.println("json to bean:" + person.getName());
}
}Execute the above code , The output is :
{"AGE":"20","userName":" Zhang San "}fastjson During operation , It's based on getter and setter The method of , Not based on Field Conduct
[email protected] In the annotations format attribute
format Property is used to specify the date format of member variables when serializing and deserializing
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;
public class Person {
private String name;
private String age;
@JSONField(format="yyyy-MM-dd HH:mm:ss")
private Date date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;
import java.util.Date;
public class Demo {
public static void main(String[] args){
// serialize
Person person=new Person();
person.setName(" Zhang San ");
person.setAge("20");
person.setDate(new Date());
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}Execute the above code , The output is :
{"age":"20","date":"2022-06-21 09:52:37","name":" Zhang San "}[email protected] In the annotations ordinal attribute
ordinal Property is used to specify the order of fields when serializing
import com.alibaba.fastjson.annotation.JSONField;
public class Person {
@JSONField(ordinal = 1)
private String name;
@JSONField(ordinal = 2)
private String age;
@JSONField(ordinal = 3)
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;
public class Demo {
public static void main(String[] args){
// serialize
Person person=new Person();
person.setName(" Zhang San ");
person.setAge("20");
person.setSex(" male ");
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}Execute the above code , The output is :
{"name":" Zhang San ","age":"20","sex":" male "}[email protected] In the annotations serialize attribute
serialize Attribute whose value is false Time indicates that the field is not serialized , Is to transform into json String does not generate this field
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;
public class Person {
private String name;
private String age;
// The specified field is not serialized , Is to transform into json String does not generate this field
@JSONField(serialize=false)
private Date date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;
import java.util.Date;
public class Demo {
public static void main(String[] args){
// serialize
Person person=new Person();
person.setName(" Zhang San ");
person.setAge("20");
person.setDate(new Date());
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}Execute the above code , The output is :
{"age":"20","name":" Zhang San "}边栏推荐
- Introduction to JDBC (I) DML operation
- 云原生架构(04)-CNCF
- 物联网开源开发平台 Shifu 开放内测!第一版技术文档发布
- Shifu, the open source development platform of the Internet of things, is open for internal testing! Release of the first version of technical documents
- JDBC入门学习(四)之Druid连接池的使用
- 第九章 APP项目测试(1)
- Stm32cube serial port uses dma+idle to receive variable length data
- 牛B程序员在“创建索引”时都会注意啥?
- 【opencv450】 图像相减、二值化、阈值分割
- STM32cube 串口使用DMA+IDLE接收不定长数据
猜你喜欢

Software project management 8.4 Software project quality plan

Is there a real part-time job online? How do college students find part-time jobs in summer?

Introduction to JDBC (III) implementation of transaction rollback function

云原生数据库是未来数据库的天下

(IntelliJ) plug in background image plus

H5 适配全面屏

Facing new challenges and becoming a better self -- an advanced technology er

Memory model of JVM principle

ES6的Array.from方法创建长度为N的undefined数组

Win软件 - (Net-Framework)已处理证书链,但是在不受信任提供程序信任的根证书中终止
随机推荐
[microservices | Nacos] Nacos realizes data isolation of multi environment and multi tenant
Webrtc[47] - a common way for webrtc to save YUV data
奇门遁甲辅助决策软件
Win11应用商店下载的软件怎么移到桌面
The tiobe programming language ranking is an indicator of the popular trend of programming languages
Cloud native architecture (04) -cncf
Composite API
JDBC introductory learning (II) encapsulation tool class
1010 Radix
Introduction to JDBC (IV) - use of Druid connection pool
STM32cube CMSIS_ V2 FreeRTOS queue usage
Zygote process
Mysql入门学习(一)之语法
MCS:离散随机变量——Uniform分布
Face recognition determination threshold
JVM原理之内存模型
Jenkins安装部署以及自动构建和发布jar应用
Mysql入门学习(三)之视图
英文字母pc是什么意思,互联网的pc指的是什么
Is there a real part-time job online? How do college students find part-time jobs in summer?