当前位置:网站首页>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]边栏推荐
猜你喜欢

IPC 通信 - IPC

自监督论文阅读笔记Reading and Writing: Discriminative and Generative Modelingfor Self-Supervised Text Recogn

ZEMAX | 如何创建简单的非序列系统

微信小程序 自定义tabBar

MATLAB自带的dwt2和wavedec2函数实现基于小波变换的自适应阈值图像边缘检测

自监督论文阅读笔记Efficient Self-supervised Vision Pretraining with Local Masked Reconstruction

自监督论文阅读笔记 TASK-RELATED SELF-SUPERVISED LEARNING FOR REMOTE SENSING IMAGE CHANGE DETECTION

Dynamic adjustment subject web system?Look at this one is enough

内网渗透信息收集

损失函数(第五周)
随机推荐
ucosII OSMemCreate()函数的解析
Practice of MySql's Sql statement (try how many you can write)
中空编码器的作用——唯样商城
ZEMAX | 探究 OpticStudio 偏振分析功能
MATLAB自带的dwt2和wavedec2函数实现基于小波变换的自适应阈值图像边缘检测
C# 数组之回溯法
神经网络基础
自监督论文阅读笔记 Self-Supervised Visual Representation Learning with Semantic Grouping
六、对比Vector、ArrayList、LinkedList有何区别?(设计、性能、安全)
g++参数说明
优雅的拦截TabLayout的点击事件
VCC(电源)和 GND(地)之间电容的作用
VSCODE 常见问题
贴片电阻的结构是怎样的?唯样商城
电容器和电池有什么不同?
【DC-5 Range Penetration】
自监督论文阅读笔记 Multi-motion and Appearance Self-Supervised Moving Object Detection
2021-03-22
交叉熵(第六周)
自监督论文阅读笔记: MoCoV2使用动量对比学习改进基线