0

次のようなjsonがあります。

[
  {"type":"child_obj_1", "message": "blah"},
  {"type":"child_obj_3", "apple": "red"},
  {"type":"child_obj_2", "banana": "fire!"},
  {"type":"doesnt_exist", "message": "blah"}
]

これらは、次のようなタイプに対応します (「doesnt_exist」タイプは存在しません)。

ParentObj
  -> ChildObj1
  -> ChildObj2
  -> ChildObj3

json リストをこれらのタイプに解析できるようにしたい。「doesnt_exist」タイプは、サイレントに失敗する可能性があります。ジャクソンを使用してこれを行うにはどうすればよいですか?

4

1 に答える 1

0

@JsonTypeInfoJackson からのアノテーションを試しましたか? 詳細については、ドキュメントを確認してください。

サンプル使用法:

 // Include Java class name ("com.myempl.ImplClass") as JSON property "class"
  @JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class")

  // Include logical type name (defined in impl classes) as wrapper; 2 annotations
  @JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
  @JsonSubTypes({com.myemp.Impl1.class, com.myempl.Impl2.class})
于 2013-05-21T18:55:02.933 に答える