当前位置:网站首页>Difference between @JsonProperty and JSONField?
Difference between @JsonProperty and JSONField?
2022-08-03 06:12:00 【Shiyu】
Purpose: It is all to solve the problem that some attribute names in the json string do not match the attribute names in JavaBean.
1.JsonProperty is located in the jackson package and used with the ObjectMapper().writeValueAsString(entity class) method to convert the entity class into a string.
Use with the ObjectMapper().readValue(string) method to convert the string into an entity class.
com.fasterxml.jackson.core jackson-databind version number
Test example:
Entity class: User.java
import com.fasterxml.jackson.annotation.JsonProperty;public class User {@JsonProperty("JsonPropertyName")private String name;private String sex;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public User(String name, String sex, Integer age) {super();this.name = name;this.sex = sex;this.age = age;}public User() {super();}@Overridepublic String toString() {return "User [name=" + name + ", sex=" + sex + ", age=" + age + "]";}}
Test method:
@Testpublic void testJsonProperty() throws IOException{User user=new User("shiyu","man",22);System.out.println(new ObjectMapper().writeValueAsString(user));String str="{\"sex\":\"man\",\"age\":22,\"JsonPropertyName\":\"shiyu\"}";System.out.println(new ObjectMapper().readValue(str, User.class).toString());}
Output result:
{"sex":"man","age":22,"JsonPropertyName":"shiyu"}User [name=shiyu, sex=man, age=22]
2.JSONField is located in the fastjson package and used with the JSON.toJSONString (entity class) method to convert the entity class into a json string.Use with the JSON.parseObject(string, entity class. class) method to convert the string into an entity class.
com.alibaba fastjson version number
Test example:
Entity class: User.java
import com.alibaba.fastjson.annotation.JSONField;public class User {@JSONField(name="JSONFieldName")private String name;private String sex;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public User(String name, String sex, Integer age) {super();this.name = name;this.sex = sex;this.age = age;}public User() {super();}@Overridepublic String toString() {return "User [name=" + name + ", sex=" + sex + ", age=" + age + "]";}}
Test method:
@Testpublic void testSONField(){User user=new User("shiyu","man",22);System.out.println(JSON.toJSONString(user));String str="{\"JSONFieldName\":\"shiyu\",\"age\":22,\"sex\":\"man\"}";System.out.println(JSON.parseObject(str, User.class).toString());}
Output result:
{"JSONFieldName":"shiyu","age":22,"sex":"man"}User [name=shiyu, sex=man, age=22]
边栏推荐
- A.1#【内存管理】——1.1.2 zone: struct zone
- ZEMAX | 如何使用渐晕系数
- 自监督论文阅读笔记Index Your Position: A Novel Self-Supervised Learning Method for Remote Sensing Images Sema
- 5. What is the difference between int and Integer?
- g++参数说明
- 常见的电容器有哪些?唯样商城
- 代码没写完,哪里有脸睡觉!17 张程序员壁纸推荐
- @JsonProperty和JSONField的区别?
- 采用Trench肖特基二极管,实现功率密度的显著提升
- 自监督论文阅读笔记 S3Net:Self-supervised Self-ensembling Network for Semi-supervised RGB-D Salient Object Det
猜你喜欢
自监督论文阅读笔记 DenseCL:Dense Contrastive Learning for Self-Supervised Visual Pre-Training
自监督论文阅读笔记 DetCo: Unsupervised Contrastive Learning for Object Detection
Windos 内网渗透之Token的使用
ZEMAX | 如何创建简单的非序列系统
滚动条 scrollbar 和scrollbar-thumb 样式
电容器和电池有什么不同?
进程间通信IPC - 信号量
IPC 通信 - IPC
SolidWorks 操作视频 | 流体分析结果演示
ZEMAX | 如何倾斜和偏心序列光学元件
随机推荐
cmdline -[command line,__fdt_pointer,initial_boot_params] boot_command_line 获取
Gradle插件与代理服务器导致Sync Project失败的问题
MySql的Sql语句的练习(试试你能写出来几道呢)
交叉熵(第六周)
JSP的基本使用
ZEMAX | How to rotate any element around any point in space
003_旭日X3派初探:利用无线串口通信控制舵机
C# Base64加密
[frp intranet penetration]
神经网络之感知机
new / malloc / delete / free之间的区别
使用JSP实现简单的登录注册功能,并且使用Session跟踪用户登录信息
5. What is the difference between int and Integer?
电子元器件的分类有哪些?
MMU 介绍-[TBL/page table work]
ucos任务调度原理
损失函数(第五周)
ZEMAX | 在OpticStudio中建立扩增实境(VR)头戴式显示器
动漫:海贼王女
opencv目标检测