これを行うための魔法の注釈はありません。あなたはあなたのPOJO
// OPTION 1 with constructor
private static class POJO{
private String name;
public POJO(String name) {
this.name = name;
}
}
// OPTION 2 with factory method
private static class POJO{
private String name;
@JsonCreator
public static POJO POJO(String name) {
POJOlink = new POJO();
link.name = name;
return link;
}
}
あなたはまた、{"externalLinks":["link1","link2"],"lectureRequirements":["requirement1","requirement2"]}
に戻ってそれをシリアル化するためにgetName()
上記@JsonValue
を使用することができますし、単一のStringのコンストラクタ/ファクトリメソッドを追加した場合LectureExternalLink
とLectureRequirement
は、文字列にデシリアライズすることができますが変更できない場合
private static class MyJsonDeserializer extends JsonDeserializer<LectureExternalLink> {
@Override
public LectureExternalLink deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException {
String value = p.getValueAsString();
LectureExternalLink val= new LectureExternalLink();
val.setName(value);
return val;
}
}
0123:何らかの理由であなたのPOJOは、私はあなたが行くための唯一の方法は、このようなカスタムデシリアライザを作成することだと思います
全デモ:
private static final String json ="{\n" +
" \"externalLinks\": [\"link1\", \"link2\"],\n" +
" \"lectureRequirements\": [\"requirement1\", \"requirement2\"]\n" +
"}";
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Lecture l = mapper.readValue(json,Lecture.class);
System.out.println(mapper.writeValueAsString(l));
}
public static class Lecture {
@JsonProperty("externalLinks")
private List<LectureExternalLink> lectureExternalLinks;
@JsonProperty("lectureRequirements")
private List<LectureRequirement> lectureRequirements;
public List<LectureExternalLink> getLectureExternalLinks() {
return lectureExternalLinks;
}
public void setLectureExternalLinks(List<LectureExternalLink> lectureExternalLinks) {
this.lectureExternalLinks = lectureExternalLinks;
}
public List<LectureRequirement> getLectureRequirements() {
return lectureRequirements;
}
public void setLectureRequirements(List<LectureRequirement> lectureRequirements) {
this.lectureRequirements = lectureRequirements;
}
}
@JsonFormat(shape = JsonFormat.Shape.STRING)
private static class LectureRequirement {
private String name;
private Integer id;
public LectureRequirement(String name) {
this.name = name;
}
@JsonValue
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
@JsonDeserialize(using = MyJsonDeserializer.class)
public static class LectureExternalLink {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@JsonValue
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private static class MyJsonDeserializer extends JsonDeserializer<LectureExternalLink> {
@Override
public LectureExternalLink deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.getValueAsString();
LectureExternalLink val= new LectureExternalLink();
val.setName(value);
return val;
}
}
あなたはIDがする何をしたいですか? – shmosel