【问题标题】:How to deal with recursive and nested data classes in Kotlin如何处理 Kotlin 中的递归和嵌套数据类
【发布时间】:2019-12-10 12:00:05
【问题描述】:

在我的应用程序中,我从需要解析它的 API 获得 JSON 响应。 JSON 如下所示:

{
"itemTemplates":
[
  {
    "type": "...",
    "properties": {
      "..": ".."
    },
    "children": [
      {
        "type": "..",
        "properties": {
          "..": ".."
        },
        "children": [
          {
            "type": ".."
          }
        ]
      },
      {
        "type": "..",
        "properties": {
          "value": ".."
        }
      },
      {
        "type": "..",
        "properties": {
          "value": ".."
        }
      }
    ]
  }
]

}

我为上述响应创建了 Kotlin 数据类,如下所示:

data class ItemTemplate(val itemTemplates: List<Component> = listOf())


data class Component(
val children: List<Children> = listOf(),
val Properties: Properties = Properties(),
val type: String = "")


data class Children(
val properties: Properties = Properties(),
val type: String = "",
val children: List<Children> = listOf())

如您所见,Children 数据类中有一个递归数据类。在 JSON 响应中,每个 Child 本身可以有多个或零个 Children。 在使用数据类时会出现问题。即,我不能遍历 Children,因为它也可能有 Children,我也需要对其进行迭代(等等)。

由于不知道哪个 Child 有 Children 以及我应该迭代多少次才能解析所有内容,所以我开始考虑使用递归函数。但是,不确定这样做是否正确以及如何进行。

有什么建议吗? 谢谢

【问题讨论】:

  • Children 的孩子看起来和 Children 完全一样吗?如果是,这很简单 - 使用 GSON。如果不是 - 很简单,编写一个 ChildrenChild 类,然后使用 GSON。
  • 谢谢,@Shark。我的问题主要是关于遍历。如何递归遍历那些嵌套对象。

标签: android kotlin data-class


【解决方案1】:

根据提供的代码,假设这是Properties模型的结构。

data class Properties(val test: String)

data class Component(
        val children: List<Children> = listOf(),
        val properties: Properties = Properties("test"),
        val type: String = ""
)

data class Children(
        val properties: Properties = Properties("test"),
        val type: String = "",
        val children: List<Children> = listOf()
)

由于不知道哪个 Child 有 Children 以及我应该迭代多少次才能解析所有内容,所以我开始考虑使用递归函数。但是,不确定这样做是否正确以及如何进行。

您不一定需要使用递归,因为 JSON 有效负载的结构是可预测的。假设您只有children 列表的一个子集。

有时候……

{
    "property": {
        "children": []    
    }
}

有时候……

{
    "property": {
        "children": [
                {
                    "property": {
                    "children": []
                }
            }
        ]
    }
}

选项 1。如果认为有必要,您可以为子子项创建不同的对象(假设它们具有不同的参数集)。

data class Children(
        val properties: Properties = Properties("test"),
        val type: String = "",
        val children: List<SubChildren>? = listOf()
)

选项 2. 使用 GSON 库。这是在 Java 和 Kotlin 中序列化数据的一种优雅方式。它将处理您的children 或其他参数在序列化期间可能存在或不存在的情况。

进一步阅读:

【讨论】:

  • 感谢您的回复。好吧,我的问题实际上是如何递归遍历所有孩子。你有什么建议吗?
  • @MehdiSatei,你有没有找到解决方案?
  • @cking24343 最简单​​的方法是使用kotlin序列化
【解决方案2】:

“我的问题实际上是如何递归遍历所有的孩子。你有什么建议吗?”

怎么样:

fun getChildrenTypes() : String =
    if (children.isEmpty()) ""
    else (type.plus(children.map { it.getChildrenTypes() }.reduce { concatString : String, element -> concatString.plus(element) }))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 2017-01-13
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    相关资源
    最近更新 更多