当前位置:网站首页>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]边栏推荐
- 常见的电容器有哪些?唯样商城
- [frp intranet penetration]
- new / malloc / delete / free之间的区别
- 三、final、finally、 finalize有什么不同?
- 深度学习理论课程第八、九、十章总结
- ARMv8 架构----armv8 类别
- pandoc -crossref插件实现markdwon文档转word后公式编号自定义
- 进程间通信IPC - 信号量
- 电子元器件的分类有哪些?
- What is parametric design, let's understand it through practical operation?| SOLIDWORKS How-To Videos
猜你喜欢
随机推荐
[XSS, file upload, file inclusion]
ZEMAX | 如何创建复杂的非序列物体
ZEMAX | 探究 OpticStudio 偏振分析功能
6. What is the difference between Vector, ArrayList and LinkedList?(design, performance, safety)
卷积神经网络入门
pandoc -crossref插件实现markdwon文档转word后公式编号自定义
Gradle插件与代理服务器导致Sync Project失败的问题
自监督论文阅读笔记 TASK-RELATED SELF-SUPERVISED LEARNING FOR REMOTE SENSING IMAGE CHANGE DETECTION
STM32启动文件的选择
进程间通讯 (IPC 技术) - 信号
中空编码器的作用——唯样商城
page fault-页异常流程
自监督论文阅读笔记 S3Net:Self-supervised Self-ensembling Network for Semi-supervised RGB-D Salient Object Det
常见的电子元器件分类介绍-唯样商城
Typora
ZEMAX | 绘图分辨率结果对光线追迹的影响
g++参数说明
KASLR-内核地址空间布局随机化
电容器和电池有什么不同?
ZEMAX | 如何围绕空间中的任何点旋转任何元素









