- 傑克遜註解教程
- 傑克遜 - 主頁
- 序列化註解
- 傑克遜 - @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
廣告