当前位置:网站首页>fastjson中的@JSONField注解
fastjson中的@JSONField注解
2022-06-23 05:36:00 【y_bccl27】
使用fastjson之前需先引入下述依賴,當前版本為1.2.75
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>JSONField中的name屬性用來指定JSON串中key的名稱
[email protected]注解作用在屬性(成員變量)上
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;
}
}序列化測試:
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("張三");
person.setAge("20");
person.setDate(new Date());
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}執行上述代碼,其輸出結果為:
{"AGE":"20","userName":"張三"}反序列化測試:
import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;
public class Demo {
public static void main(String[] args){
String jsonStr="{\"AGE\":\"20\",\"userName\":\"張三\"}";
Person person = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Person.class);
System.out.println("json to bean:" + person.getName());
}
}執行上述代碼,其輸出結果為:
json to bean:張三@JSONField作用在Field時,其name不僅定義了輸出的名稱,同時也定義了輸入key的名稱
[email protected]注解作用在方法上
import com.alibaba.fastjson.annotation.JSONField;
public class Person {
private String name;
private String age;
// 針對的是序列化操作
@JSONField(name = "userName")
public String getName() {
return name;
}
// 針對的是反序列化操作
@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){
// 序列化
Person person=new Person();
person.setName("張三");
person.setAge("20");
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
// 反序列化
// String jsonStr="{\"AGE\":\"20\",\"userName\":\"張三\"}";
// Person person = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Person.class);
// System.out.println("json to bean:" + person.getName());
}
}執行上述代碼,其輸出結果為:
{"AGE":"20","userName":"張三"}fastjson在進行操作時,是根據getter和setter的方法進行的,並不是依據Field進行
[email protected]注解中的format屬性
format屬性用於規定序列化和反序列化時成員變量的日期格式
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){
// 序列化
Person person=new Person();
person.setName("張三");
person.setAge("20");
person.setDate(new Date());
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}執行上述代碼,其輸出結果為:
{"age":"20","date":"2022-06-21 09:52:37","name":"張三"}[email protected]注解中的ordinal屬性
ordinal屬性用於規定序列化時字段的順序
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){
// 序列化
Person person=new Person();
person.setName("張三");
person.setAge("20");
person.setSex("男");
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}執行上述代碼,其輸出結果為:
{"name":"張三","age":"20","sex":"男"}[email protected]注解中的serialize屬性
serialize屬性其取值為false時錶示該字段不進行序列化,就是轉化為json字符串時不生成該字段
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;
public class Person {
private String name;
private String age;
// 指定字段不進行序列化,就是轉化為json字符串時不生成該字段
@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){
// 序列化
Person person=new Person();
person.setName("張三");
person.setAge("20");
person.setDate(new Date());
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}執行上述代碼,其輸出結果為:
{"age":"20","name":"張三"}边栏推荐
- JDBC introductory learning (II) encapsulation tool class
- QT QWidget nesting relative position acquisition (QT drawing nesting)
- 99 multiplication table bat
- JDBC入门学习(三)之事务回滚功能的实现
- Facing new challenges and becoming a better self -- an advanced technology er
- Database connection exception: create connection error, url: jdbc: mysql://ip/ Database name, errorcode 0, state 08s01 problem handling
- [opencv450] inter frame difference method
- Implementation of MySQL custom sequence number
- GO语言-自定义error
- Missing essential plugin
猜你喜欢

SIFT feature point extraction
![[leetcode] longest increasing subsequence problem and its application](/img/e0/d666dccec1f65eed61fce83ac2190a.png)
[leetcode] longest increasing subsequence problem and its application

【opencv450】帧间差分法

Face recognition determination threshold

Win11应用商店一直转圈解决办法

STC 32比特8051單片機開發實例教程 一 開發環境搭建

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

Lihongyi, machine learning 5 Tips for neural network design

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

Win11不能录制音频怎么办?Win11无法录入声音的解决方法
随机推荐
Get bat command results in bat
Is there a real part-time job online? How do college students find part-time jobs in summer?
MCS: continuous random variable lognormal distribution
WebRTC[47] - WebRTC 保存 YUV 数据的常用方式
ES6的Array.from方法创建长度为N的undefined数组
CF [1700d] D. River locks (DP, bisection, Mathematics)
(IntelliJ) plug in background image plus
STM32cube CMSIS_V2 freeRTOS Queue 队列使用
英集芯推出4串锂电池100W移动电源升降压方案SoC芯片IP5389
Introduction to MySQL (II) sub query + Association
Go 分组 & 排序
Mysql入门学习(一)之语法
gis利器之Gdal(三)gdb数据读取
软件项目管理 8.4.软件项目质量计划
Array The from method creates an undefined array of length n
Mathematical analysis_ Notes_ Chapter 1: set and mapping
架构师之路,从「存储选型」起步
抽奖 ddd 代码
Export PDF with watermark
小时候 觉得爸爸就是天 无所不能~