如何使用Moshi反序列化来平整多态列表

时间:2019-04-18 08:10:33

标签: android moshi

我正在尝试拼合以下Json

[
  {
    "type": 1,
    "field": ""
  },
  {
    "type": 2,
    "field": "",
    "other_field": ""
  },
  {
    "type": 3,
    "field": "",
    "list_field": [
      {
        "type": 4,
        "some_field": ""
      },
      {
        "type": 4,
        "some_field": ""
      }
    ]
  }
]

我想要获得的是以下结构:

[
  {
    "type": 1,
    "field": ""
  },
  {
    "type": 2,
    "field": "",
    "other_field": ""
  },
  {
    "type": 3,
    "field": ""
  },
  {
    "type": 4,
    "some_field": ""
  },
  {
    "type": 4,
    "some_field": ""
  }
]

由于所有项目均从同一个父类继承,因此我尝试了PolymorphicJsonAdapterFactory和自定义的JsonAdapter.Factory,但均未成功。

在Moshi中有办法做到这一点吗?

0 个答案:

没有答案