当前位置:网站首页>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":"張三"}边栏推荐
- 手机无线充电双线圈15W方案SOC英集芯IP6809
- 弱者易怒如虎,强者平静如水,真正厉害的人早已戒掉了情绪
- View of MySQL introductory learning (III)
- Face recognition determination threshold
- MCS: continuous random variable - student's t distribution
- Markdown add background color to the picture
- (IntelliJ) plug in background image plus
- [opencv450] image subtraction, binarization, threshold segmentation
- Zygote process
- STC 32位8051单片机开发实例教程 一 开发环境搭建
猜你喜欢

基于SSM框架的借阅图书管理系统
![[opencv450] image subtraction, binarization, threshold segmentation](/img/2c/60d64f3cdcc4ad8f95369d30ad2cdd.png)
[opencv450] image subtraction, binarization, threshold segmentation

Markdown add background color to the picture

Qt QWidget嵌套相对位置获取 (qt 画线 嵌套)

H5 适配全面屏

markdown给图片加背景色

Introduction to JDBC (I) DML operation

MCS: continuous random variable - student's t distribution

网上有真实的兼职吗?大学生怎么找暑期兼职?

Un processus GC complet pour le principe JVM
随机推荐
MCS: continuous random variable lognormal distribution
App hangs~
网上有真实的兼职吗?大学生怎么找暑期兼职?
JDBC introductory learning (II) encapsulation tool class
STM32 clock tree misconfiguration causes boot to enter hard interrupt
Build a gocd environment
JVM原理之完整的一次GC流程
Mysql入门学习(三)之视图
面对新的挑战,成为更好的自己--进击的技术er
Introduction to unityshader -- rendering optimization technology in unity (IV)
电脑开机显示器黑屏是什么原因,电脑显示器黑屏怎么办
2022年中国重卡智能化升级专题研究
Get bat command results in bat
Markdown add background color to the picture
MCS:连续随机变量——LogNormal分布
Current situation and development of containerization technology under the cloud native trend
hash---------history
Go language - custom error
Meteorological mapping software panoply tutorial (updated from time to time)
Introduction to MySQL (II) sub query + Association