2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.
@JsonProperty(value = "isReceipt")
public boolean isReceipt() {
return isReceipt;
}
@JsonProperty(value = "isExamine")
public boolean isExamine() {
return isExamine;
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>版本号</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>版本号</version>
</dependency>
@JSONproperty annotation is used forAttributesabove
The @JSONField annotation can be usedget, set, and propertiesabove
The @JSONField annotation is simpler to use. The default value of the annotation is the same as the attribute name, while @JsonProperty requires manual specification of the attribute name.
The @JSONField annotation supports more attribute mapping options, such as date format during serialization, handling of null values, etc.
@JSONField annotation has faster performance, because fastjson itself is a high-performance JSON processing library
When using the Jackson framework, you can only use the @JsonProperty annotation, not the @JSONField annotation.
If you are using a newly createdspringboot project, Jackson serialization is the default, and annotations can be used directly on the attributes.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>版本号</version>
</dependency>
Entity class: User.java
import com.fasterxml.jackson.annotation.JsonProperty;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@JsonProperty("JsonPropertyName")
private String name;
private String sex;
private Integer age;
}
Test Methods:
@Test
public void testJsonProperty() throws IOException{
//bean ---> json
User user=new User("zhangsan","man",22);
System.out.println(new ObjectMapper().writeValueAsString(user));
//json ---> bean
String str="{"sex":"man","age":22,"JsonPropertyName":"zhangsan"}";
System.out.println(new ObjectMapper().readValue(str, User.class).toString());
}
Test Results:
{"sex":"man","age":22,"JsonPropertyName":"zhangsan"}
User [name=zhangsan, sex=man, age=22]
It can be seen that after the bean is converted into a json string, the bean property name annotated with @JsonProperty has been replaced with the specified property name: JsonPropertyName;
After the json string is converted to a bean, the property name specified by the @JsonProperty annotation has been replaced with the bean property name: name;
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>版本号</version>
</dependency>
Entity class: User.java
import com.alibaba.fastjson.annotation.JSONField;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@JSONField(name="JSONFieldName")
private String name;
private String sex;
private Integer age;
}
Test Methods:
@Test
public void testSONField(){
//bean ---> json
User user=new User("zhangsan","man",22);
System.out.println(JSON.toJSONString(user));
//json ---> bean
String str="{"JSONFieldName":"zhangsan","age":22,"sex":"man"}";
System.out.println(JSON.parseObject(str, User.class).toString());
}
Test Results:
{"JSONFieldName":"zhangsan","age":22,"sex":"man"}
User [name=zhangsan, sex=man, age=22]
The format attribute can be used to specify the format of date type attributes and the format of converting numeric types to string types.
public class User {
private String name;
private int age;
@JSONField(format = "yyyy-MM-dd")
private Date birthday;
@JSONField(format = "#,###.00")
private double salary;
}
public class User {
@JSONField(serialize = false)
private int userId;
private String name;
private int age;
}
In the above example, the userId property is excluded from serialization using the @JSONField annotation.
public class User {
private int userId;
private String name;
@JSONField(deserialize = false)
private int age;
}
In the above example, the age property is excluded from deserialization using the @JSONField annotation.
The ordinal attribute can be used to specify the order of attributes.
public class User {
@JSONField(ordinal = 2)
private String name;
@JSONField(ordinal = 1)
private int age;
}
In the above example, the @JSONField annotation is used to specify the order of the age attribute as 1 and the order of the name attribute as 2.
The defaultValue attribute can be used to specify the default value of a property in a Java object.
public class User {
@JSONField(defaultValue = "0")
private int userId;
@JSONField(defaultValue = "N/A")
private String name;
private int age;
}
In the above example, the @JSONField annotation is used to specify the default value of the userId attribute as 0 and the default value of the name attribute as "N/A"
public class User {
private int userId;
@JSONField(type = FieldType.STRING)
private int age;
}
In the above example, the @JSONField annotation is used to specify that the type of the age attribute is a string type.