Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何处理Kotlin中的递归和嵌套数据类_Android_Kotlin_Data Class - Fatal编程技术网

Android 如何处理Kotlin中的递归和嵌套数据类

Android 如何处理Kotlin中的递归和嵌套数据类,android,kotlin,data-class,Android,Kotlin,Data Class,在我的应用程序中,我从一个API获得一个JSON响应,我需要解析它。 JSON如下所示: { "itemTemplates": [ { "type": "...", "properties": { "..": ".." }, "children": [ { "type": "..", "properties": { "..": ".." }, "chil

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

{
"itemTemplates":
[
  {
    "type": "...",
    "properties": {
      "..": ".."
    },
    "children": [
      {
        "type": "..",
        "properties": {
          "..": ".."
        },
        "children": [
          {
            "type": ".."
          }
        ]
      },
      {
        "type": "..",
        "properties": {
          "value": ".."
        }
      },
      {
        "type": "..",
        "properties": {
          "value": ".."
        }
      }
    ]
  }
]
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())
}

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

{
"itemTemplates":
[
  {
    "type": "...",
    "properties": {
      "..": ".."
    },
    "children": [
      {
        "type": "..",
        "properties": {
          "..": ".."
        },
        "children": [
          {
            "type": ".."
          }
        ]
      },
      {
        "type": "..",
        "properties": {
          "value": ".."
        }
      },
      {
        "type": "..",
        "properties": {
          "value": ".."
        }
      }
    ]
  }
]
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())
数据类ItemTemplate(val itemTemplates:List=listOf())
数据类组件(
val子项:List=listOf(),
val属性:属性=属性(),
val类型:String=“”)
数据类子类(
val属性:属性=属性(),
val type:String=“”,
val子项:List=listOf())
正如您所看到的,在子数据类中有一个递归数据类。在JSON响应中,每个子级本身可以有多个或零个子级。 在使用数据类时会出现问题。i、 我不能循环遍历子对象,因为它可能也有子对象,我也需要遍历这些子对象(等等)

因为不知道哪个孩子有孩子,也不知道我应该迭代多少次才能解析所有东西,所以我开始考虑使用递归函数。但是,不确定这样做是否正确以及如何做

有什么建议吗?
谢谢

基于提供的代码,让我们假设这是
属性
模型的结构

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()
)
有时

{
    "property": {
        "children": []    
    }
}
{
    "property": {
        "children": [
                {
                    "property": {
                    "children": []
                }
            }
        ]
    }
}
备选案文1。如果认为有必要(假设子对象具有不同的参数集),可以为子对象创建不同的对象

数据类子类(
val属性:属性=属性(“测试”),
val type:String=“”,
val子项:列表?=listOf()
)
备选案文2。使用图书馆。这是关于如何用Java和Kotlin序列化数据的优雅方法之一。它将处理在序列化过程中可能存在或不存在
子项
或其他参数的情况

进一步阅读:

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

      比如说:

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

      孩子们的孩子看起来和孩子们完全一样吗?如果是,它很简单-使用GSON。如果没有-很简单,写一个ChildrenChild类,然后使用GSON。谢谢,@Shark。我的问题主要是关于遍历。如何递归遍历这些嵌套对象。感谢您的回复。我的问题是如何递归遍历所有的子对象。你有什么建议吗?@MehdiSatei,你有没有找到解决这个问题的方法?@Cock24343最简单的方法是使用kotlin序列化