You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin中如何遍历API返回的data class对象获取非0值属性名

Kotlin实现data class非零属性名输出方案

方案选择结论

不需要提前将对象转换为Map结构。转Map需要额外遍历一次属性做键值映射,属于冗余操作,直接遍历类属性判断即可,有两种常用实现方式可按需选择。


方案1:反射遍历(代码简洁,推荐)

利用Kotlin反射能力直接遍历类的所有属性,无需手动写重复判断逻辑,后续增减属性时不需要修改这段逻辑。
如果项目没有引入Kotlin反射依赖,先在Gradle配置中添加:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-reflect:${对应项目的Kotlin版本号}"
}

实现代码:

import kotlin.reflect.full.memberProperties

// 扩展函数,获取所有值不为0的充电桩类型属性名
fun NumberOfChargers.getNonZeroChargerTypeNames(): List<String> {
    return this::class.memberProperties
        .filter { property ->
            // 筛选Int类型、数值不等于0的属性
            property.returnType.classifier == Int::class && (property.get(this) as Int) != 0
        }
        .map { it.name }
}

// 调用示例
fun main() {
    val chargerData = NumberOfChargers(
        BusACGBTtype2=0, BusACType2=0, CarACType2=11, BusDCCCS2=0,
        CarACGBT=1, CarACType1=1, CarDCCCS1=1, CarDCCCS2=0,
        CarDCCHAdeMO=8, CarDCChaoji=0, CarDCGBT=1,
        MotorcycleACDefault=0, MotorcycleACType1=0, MotorcycleACType2=0
    )
    // 逐个打印结果
    chargerData.getNonZeroChargerTypeNames().forEach { println(it) }
    /* 输出:
    CarACType2
    CarACGBT
    CarACType1
    CarDCCCS1
    CarDCCHAdeMO
    CarDCGBT
    */
}

如果需要输出@SerializedName注解对应的接口原始字段名(比如Car-AC-Type2而非变量名CarACType2),调整映射逻辑即可:

import com.google.gson.annotations.SerializedName
import kotlin.reflect.full.memberProperties

fun NumberOfChargers.getNonZeroChargerApiNames(): List<String> {
    return this::class.memberProperties
        .filter { prop ->
            prop.returnType.classifier == Int::class && (prop.get(this) as Int) != 0
        }
        .map { prop ->
            prop.annotations.filterIsInstance<SerializedName>()
                .firstOrNull()?.value ?: prop.name
        }
}

方案2:硬编码判断(无额外依赖,性能最高)

如果项目不想引入反射依赖,可以直接写固定判断逻辑,运行效率最高,缺点是属性增减时需要同步修改这段代码:

fun NumberOfChargers.getNonZeroChargerTypeNamesNoReflect(): List<String> {
    val result = mutableListOf<String>()
    if (BusACGBTtype2 != 0) result.add("BusACGBTtype2")
    if (BusACType2 != 0) result.add("BusACType2")
    if (CarACType2 != 0) result.add("CarACType2")
    if (BusDCCCS2 != 0) result.add("BusDCCCS2")
    if (CarACGBT != 0) result.add("CarACGBT")
    if (CarACType1 != 0) result.add("CarACType1")
    if (CarDCCCS1 != 0) result.add("CarDCCCS1")
    if (CarDCCCS2 != 0) result.add("CarDCCCS2")
    if (CarDCCHAdeMO != 0) result.add("CarDCCHAdeMO")
    if (CarDCChaoji != 0) result.add("CarDCChaoji")
    if (CarDCGBT != 0) result.add("CarDCGBT")
    if (MotorcycleACDefault != 0) result.add("MotorcycleACDefault")
    if (MotorcycleACType1 != 0) result.add("MotorcycleACType1")
    if (MotorcycleACType2 != 0) result.add("MotorcycleACType2")
    return result
}

内容的提问来源于stack exchange,提问作者siavash parsa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 08:00:36