当前位置:网站首页>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]
边栏推荐
猜你喜欢
KASLR-内核地址空间布局随机化
微信小程序 自定义tabBar
自监督论文阅读笔记SELF-SUPERVISED SPECTRAL MATCHING NETWORK FOR HYPERSPECTRAL TARGET DETECTION
Convolutional Nerual Nertwork(CNN)
POE交换机全方位解读(中)
【第四周】MobileNet和HybridSN
opencv透视变化
ZEMAX | How to rotate any element around any point in space
自监督论文阅读笔记 Self-supervised Learning in Remote Sensing: A Review
自监督论文阅读笔记 Multi-motion and Appearance Self-Supervised Moving Object Detection
随机推荐
解决Gradle Download缓慢的百种方法
自监督论文阅读笔记SELF-SUPERVISED SPECTRAL MATCHING NETWORK FOR HYPERSPECTRAL TARGET DETECTION
enum和enum class的区别
寄存器常见指令
设备树解析源码分析<devicetree>-1.基础结构
2021-04-30
MCU接收串口字符型数据转换成数据型数据
POE交换机全方位解读(中)
Hook初探索
new / malloc / delete / free之间的区别
自监督论文阅读笔记FIAD net: a Fast SAR ship detection network based on feature integration attention and self
【第四周】MobileNet和HybridSN
002_旭日X3派初探:TogetherROS安装
STM32启动文件的选择
八、抽象类的接口的区别
2021-03-22
Dynamic adjustment subject web system?Look at this one is enough
关于芯片你了解吗?
使用JSP实现简单的登录注册功能,并且使用Session跟踪用户登录信息
自监督论文阅读笔记 S3Net:Self-supervised Self-ensembling Network for Semi-supervised RGB-D Salient Object Det