当前位置:网站首页>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"}边栏推荐
- Is there a real part-time job online? How do college students find part-time jobs in summer?
- JDBC入门学习(二)之封装工具类
- Missing essential plugin
- How much disk IO will actually occur for a byte of the read file?
- Introduction to MySQL (I) grammar
- Build a gocd environment
- MCS:离散随机变量——Bernoulli分布
- CF [1700d] D. River locks (DP, bisection, Mathematics)
- 关于DOS/DDOS攻击和防御
- STC 32-bit 8051 MCU development example tutorial I development environment construction
猜你喜欢

(IntelliJ) plug in background image plus

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

Un processus GC complet pour le principe JVM

架构师之路,从「存储选型」起步

The tiobe programming language ranking is an indicator of the popular trend of programming languages

Konva series tutorial 1:what is konva?

Introduction to JDBC (III) implementation of transaction rollback function

Software project management 8.4 Software project quality plan

人脸识别 确定阈值

GO语言-自定义error
随机推荐
Go language -panic and recover
抽奖 ddd 代码
Win软件 - (Net-Framework)已处理证书链,但是在不受信任提供程序信任的根证书中终止
Ams:startactivity desktop launch application
Jenkins installs and deploys and automatically builds and publishes jar applications
STM32cube 串口使用DMA+IDLE接收不定长数据
[opencv450] image subtraction, binarization, threshold segmentation
LeetCode-1757. Recyclable and low-fat products_ SQL
After the idea code is developed, the code is submitted. If the branch is found to be incorrect after submission, how can I withdraw it
When I was young, I thought my father was omnipotent
MCS: continuous random variable chi square distribution
Win11应用商店下载的软件怎么移到桌面
数据库连接异常:create connection error, url: jdbc:mysql://ip/数据库名, errorCode 0, state 08S01问题处理
关于DOS/DDOS攻击和防御
Introduction to MySQL (II) sub query + Association
Get bat command results in bat
今日睡眠质量记录80分
B-string value (string DP) of the 16th Northeast College Students' Programming Competition (warm-up)
左侧固定,右侧自适应 三种实现办法(Flex,float + BFC ,float-margin-left)
MCS: discrete random variable