簡體   English   中英

使用 Jackson 反序列化 object

[英]Deserialize object with Jackson

I would like to know if there is an option in FasterXML Jackson (or any other pure way) to deserialize JSON in such way, that when I have JSON response from API like this:

{"id": "1234", "name": "name1"}

然后自動將其動態映射到 class 層次結構,如下所示:

class Person (
   val sellerId: SellerId,
   val name: String
)

class SellerId (
   val id: String
)

實際上,我很有趣,主要是如何從頂級“id”進行動態反序列化:字符串到嵌套的“sellerid”,即包含此 id 的 object。

你需要:

  1. 定義一個String -argument 構造函數/工廠方法來反序列SellerId class 的String值。
  2. Annotate sellerId property with com.fasterxml.jackson.annotation.JsonAlias or com.fasterxml.jackson.annotation.JsonProperty annotations. 類似於: @JsonProperty("id") val sellerId: SellerId

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM