简体   繁体   English

Kotlinx 序列化 - 如何为未知字段名写入数据 class

[英]Kotlinx serialization - How to write a data class for an unknown field name

I try to write a data class for the following JSON response from a public API:我尝试为来自公共 API 的以下 JSON 响应写入数据 class:

{
  "error": [],
  "result": {
    "AAVE": {
      "aclass": "currency",
      "altname": "AAVE",
      "decimals": 10,
      "display_decimals": 5
    },
    "ADA": {
      "aclass": "currency",
      "altname": "ADA",
      "decimals": 8,
      "display_decimals": 6
    },
    "ALGO": {
      "aclass": "currency",
      "altname": "ALGO",
      "decimals": 8,
      "display_decimals": 5
    },
    "ANT": {
      "aclass": "currency",
      "altname": "ANT",
      "decimals": 10,
      "display_decimals": 5
    }  
  }
}

My data class looks like:我的数据 class 看起来像:

@Serializable
data class AssetInfo (
        @SerialName("error")
        val error: List<String>?,

        @SerialName("result")
        val result: Result,

)

@Serializable
data class Result(
/*
Here is the problem, because the field "asset_name" does not exist.
*/
        @SerialName("asset_name")
        val asset_name: Asset,

)

@Serializable
data class Asset(
        @SerialName("altname")
        val altname : String,

        @SerialName("aclass")
        val aclass  : String,

        @SerialName("decimals")
        val decimals  : String,

        @SerialName("display_decimals")
        val display_decimals  : String,
)

In the data class "Result" the field name, that I declared as "asset_name" is different for every entry.在数据 class“结果”中,我声明为“asset_name”的字段名称对于每个条目都不同。 How does the data class have to look like?数据 class 的外观如何? Can someone help please?有人可以帮忙吗?

There is no need in Result class. Result class 中不需要。 Your data classes should look like this:您的数据类应如下所示:

@Serializable
data class AssetInfo(
    @SerialName("error")
    val error: List<String>?,

    @SerialName("result")
    val result: Map<String, Asset>
)

@Serializable
data class Asset(
    @SerialName("altname")
    val altname: String,

    @SerialName("aclass")
    val aclass: String,

    @SerialName("decimals")
    val decimals: Int,

    @SerialName("display_decimals")
    val display_decimals: Int
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 kotlinx.serialization 忽略未知类型? - How to ignore unknown types with kotlinx.serialization? 如何使用 kotlinx.serialization 序列化嵌套的 class - How Serialize nested class with kotlinx.serialization 如何将数据 Class 转换为 HashMap 用于 Kotlin 中的 Firestore 查看 ZA559B87068921EEC05086CE54x8.Eserialization75086CE54x85。 - How to convert Data Class to HashMap for Firestore in Kotlin View Model, kotlinx.serialization not working 如何使用 kotlinx.serialization 将具有默认值的 Kotlin 数据类序列化为 JSON? - How to serialize Kotlin data-class with default values into JSON using kotlinx.serialization? 在 Kotlinx 序列化中将字段设为可选 - Make a field optional in Kotlinx serialization 如何使用 Kotlinx 序列化对类型化类进行编码? - How can I encode a typed class with Kotlinx Serialization? 如何使用 kotlinx 序列化序列化 kotlin 密封 class 与 open val - How to serialize kotlin sealed class with open val using kotlinx serialization 如何使用 kotlinx.serialization 将库类序列化为 Protobuf? - How to serialize a library class to Protobuf with kotlinx.serialization? 我如何使用 kotlinx.serialization 序列化一个通用的密封 class - How do i serialize a generic sealed class with kotlinx.serialization 如何结合 Kotlinx 序列化在 Kotlin 中正确使用 class inheritance - How to properly use class inheritance in Kotlin in combination with Kotlinx Serialization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM