我已經JSON結構類似:反序列化JSON陣列地圖使用傑克遜
{
"id" : "123",
"name" : [ {
"stuff" : [ {
"id" : "234",
"name" : "Bob"
}, {
"id" : "345",
"name" : "Sally"
} ]
} ]
}
,我想映射到數據結構如下:
MyInterface1
@Value.Immutable
@JsonSerialize(as = ImmutableMyInterface1.class)
@JsonDeserialize(as = ImmutableMyInterface1.class)
public interface MyInterface1 {
String id();
@JsonDeserialize(using = MyInterface1Deserializer.class)
List<MyInterface2> name();
}
MyInterface2
@Value.Immutable
@JsonSerialize(as = ImmutableMyInterface2.class)
@JsonDeserialize(as = ImmutableMyInterface2.class)
public interface MyInterface2 {
@JsonDeserialize(using = StuffDeserializer.class)
Map<String, MyInterface3> stuff();
}
MyInterface3
@Value.Immutable
@JsonSerialize(as = ImmutableMyInterface3.class)
@JsonDeserialize(as = ImmutableMyInterface3.class)
public interface MyInterface3 {
String id();
String name();
}
我使用的是readValue(stringWithJson,MyInterface1.class)
的ObjectMapper映射這個JSON來MyInterface1,應繼續環比下滑至MyInterface3。當我在MyInterface2中使用列表時,此設置正在工作,即List<MyInterface3> name();
但是,我希望這是一個映射而不是列表,理想情況下內部JSON中的「id」作爲鍵。這將允許我使用下面的語法獲取值: MyInterface1.get(0).MyInterface2.get("id1").name();
的問題是,試圖創建一個自定義StuffDeserializer.class的時候,我發現了錯誤:試圖做的時候 Can not deserialize instance of com.foo.ImmutableMyInterface2$Json out of START_ARRAY token
:
public Map<String, MyInterface3> deserialize(JsonParser jsonParser, DeserializationContext ctxt)
throws IOException {
MyInterface2 foo = Unmarshaller.OBJECT_MAPPER.readValue(jsonParser, MyInterface2.class); // error here
...
我想這是因爲傑克遜期待「stuff」成爲JSON數組的List的原因。將此JSON反序列化爲使用內部JSON值作爲關鍵字的映射的最佳方法是什麼?