- 杰克逊注解教程
- 杰克逊 - 主页
- 序列化注解
- 杰克逊 - @JsonAnyGetter
- 杰克逊 - @JsonGetter
- @JsonPropertyOrder
- 杰克逊 - @JsonRawValue
- 杰克逊 - @JsonValue
- 杰克逊 - @JsonRootName
- 杰克逊 - @JsonSerialize
- 反序列化注解
- 杰克逊 - @JsonCreator
- 杰克逊 - @JacksonInject
- 杰克逊 - @JsonAnySetter
- 杰克逊 - @JsonSetter
- 杰克逊 - @JsonDeserialize
- @JsonEnumDefaultValue
- 属性包含注解
- @JsonIgnoreProperties
- 杰克逊 - @JsonIgnore
- 杰克逊 - @JsonIgnoreType
- 杰克逊 - @JsonInclude
- 杰克逊 - @JsonAutoDetect
- 类型处理注解
- 杰克逊 - @JsonTypeInfo
- 杰克逊 - @JsonSubTypes
- 杰克逊 - @JsonTypeName
- 常规注解
- 杰克逊 - @JsonProperty
- 杰克逊 - @JsonFormat
- 杰克逊 - @JsonUnwrapped
- 杰克逊 - @JsonView
- @JsonManagedReference
- @JsonBackReference
- 杰克逊 - @JsonIdentityInfo
- 杰克逊 - @JsonFilter
- 杂项
- 自定义注释
- MixIn 注释
- 禁用注释
- 杰克逊注释资源
- 杰克逊 - 快速指南
- 杰克逊 - 有用资源
- 杰克逊 - 讨论
杰克逊注解 - @JsonAnySetter
@JsonAnySetter 允许 setter 方法使用 Map,然后像处理其他属性一样以类似的方式反序列化 JSON 中的附加属性。
示例 @JsonAnySetter
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonTester {
public static void main(String args[]){
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"RollNo\" : \"1\",\"Name\" : \"Mark\"}";
try {
Student student = mapper.readerFor(Student.class).readValue(jsonString);
System.out.println(student.getProperties().get("Name"));
System.out.println(student.getProperties().get("RollNo"));
}
catch (IOException e) {
e.printStackTrace();
}
}
}
class Student {
private Map<String, String> properties;
public Student(){
properties = new HashMap<>();
}
public Map<String, String> getProperties(){
return properties;
}
@JsonAnySetter
public void add(String property, String value){
properties.put(property, value);
}
}
输出
Mark 1
广告