当前位置:网站首页>Jsonfield annotation in fastjson
Jsonfield annotation in fastjson
2022-06-23 05:36:00 【Y Bccl27】
UtiliserfastjsonLes dépendances suivantes doivent être introduites avant,La version actuelle est1.2.75
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>JSONFieldDansnamePropriété utilisée pour spécifierJSONEn chaînekeyNom de
[email protected] sur les propriétés(Variable membre)Allez.
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;
}
}Test de sérialisation:
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);
}
}Exécuter le code ci - dessus,Le résultat est:
{"AGE":"20","userName":"Zhang San"}Test de désrialisation:
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());
}
}Exécuter le code ci - dessus,Le résultat est:
json to bean:Zhang San@JSONFieldAgit surFieldHeure,Lename Non seulement le nom de la sortie est défini , L'entrée est également définie keyNom de
[email protected]'annotation agit sur la méthode
import com.alibaba.fastjson.annotation.JSONField;
public class Person {
private String name;
private String age;
// Pour les opérations de sérialisation
@JSONField(name = "userName")
public String getName() {
return name;
}
// Pour une opération de désrialisation
@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){
// Sérialisation
Person person=new Person();
person.setName("Zhang San");
person.setAge("20");
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
// Désérialisation
// String jsonStr="{\"AGE\":\"20\",\"userName\":\"Zhang San\"}";
// Person person = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Person.class);
// System.out.println("json to bean:" + person.getName());
}
}Exécuter le code ci - dessus,Le résultat est:
{"AGE":"20","userName":"Zhang San"}fastjsonPendant l'opération,C'est basé surgetterEtsetterLa méthode de,Ce n'est pas une baseFieldEn cours
[email protected] les notesformatPropriétés
format Propriété utilisée pour spécifier le format de date des variables membres lors de la sérialisation et de la désérialisation
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){
// Sérialisation
Person person=new Person();
person.setName("Zhang San");
person.setAge("20");
person.setDate(new Date());
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}Exécuter le code ci - dessus,Le résultat est:
{"age":"20","date":"2022-06-21 09:52:37","name":"Zhang San"}[email protected] les notesordinalPropriétés
ordinal Propriété utilisée pour spécifier l'ordre des champs lors de la sérialisation
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){
// Sérialisation
Person person=new Person();
person.setName("Zhang San");
person.setAge("20");
person.setSex("Hommes");
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}Exécuter le code ci - dessus,Le résultat est:
{"name":"Zhang San","age":"20","sex":"Hommes"}[email protected] les notesserializePropriétés
serialize La valeur de la propriété est false Indique que le champ n'est pas sérialisé , Est de se transformer en json Ce champ n'est pas généré lorsque la chaîne est
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;
public class Person {
private String name;
private String age;
// Le champ spécifié n'est pas sérialisé , Est de se transformer en json Ce champ n'est pas généré lorsque la chaîne est
@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){
// Sérialisation
Person person=new Person();
person.setName("Zhang San");
person.setAge("20");
person.setDate(new Date());
String jsonStr = JSONObject.toJSONString(person);
System.out.println(jsonStr);
}
}Exécuter le code ci - dessus,Le résultat est:
{"age":"20","name":"Zhang San"}边栏推荐
- CF [1700d] D. River locks (DP, bisection, Mathematics)
- Meteorological mapping software panoply tutorial (updated from time to time)
- Win software - (net framework) processed the certificate chain but terminated in a root certificate that is not trusted by the trusted provider
- Win11 app store keeps turning around solution
- Win软件 - (Net-Framework)已处理证书链,但是在不受信任提供程序信任的根证书中终止
- Get bat command results in bat
- What do Niu B programmers pay attention to when "creating an index"?
- STC 32 Bit 8051 Single Chip Computer Development Example Tutorial one development environment
- Ams:startactivity desktop launch application
- 英集芯ip6806无线充电方案5W过Qi认证外围精简14颗器件
猜你喜欢

【opencv450】帧间差分法

Introduction to JDBC (III) implementation of transaction rollback function

Go language -panic and recover

STM32 clock tree misconfiguration causes boot to enter hard interrupt

Pkav simple blasting

JDBC入门学习(一)之DML操作

面对新的挑战,成为更好的自己--进击的技术er

【opencv450】 图像相减、二值化、阈值分割

软件项目管理 8.4.软件项目质量计划

Missing essential plugin
随机推荐
Xa mode explanation and code implementation of four Seata modes
Jenkins installs and deploys and automatically builds and publishes jar applications
[microservices | Nacos] Nacos realizes data isolation of multi environment and multi tenant
STC 32比特8051單片機開發實例教程 一 開發環境搭建
[microservices | Nacos] list of issues related to the Nacos version
Introduction to JDBC (III) implementation of transaction rollback function
IDEA 代码开发完毕后,提交代码,提交后发现分支不对,怎么撤回
gis利器之Gdal(三)gdb数据读取
Konva series tutorial 1:what is konva?
物联网开源开发平台 Shifu 开放内测!第一版技术文档发布
JVM原理之完整的一次GC流程
LeetCode-1757. Recyclable and low-fat products_ SQL
Memory model of JVM principle
JDBC入门学习(三)之事务回滚功能的实现
Design and implementation of spark offline development framework
关于DOS/DDOS攻击和防御
Win11 app store keeps turning around solution
Mysql入门学习(二)之子查询+关联
Facing new challenges and becoming a better self -- an advanced technology er
Current situation and development of containerization technology under the cloud native trend