首页 > 解决方案 > 使用对象映射器(在 kotlin 中)从 S3 读取没有键名的 json 文件(大约 180 MB)到数据类

问题描述

我在 s3 中的示例 json 如下所示:

{"380682":{"ids":[380682, 765894, 9875086]},"1995539":{"ids":[1995539, 234567, 987654]}}

我想将其映射到活动列表,其中:

data class Activities(
    val key: String,
    val values: List<String>
)

我的应用程序要求我只使用Object Mapper

目前我正在这样做,但它需要的时间太长:

val activitiesList = mutableListOf<Activities>()
    val response: Map<String, Map<String, List<String>>>
    val reader: Reader = InputStreamReader(S3ObjectInputStream, StandardCharsets.UTF_8)
    val jsonNode: JsonNode

    reader.use { jsonNode = objectMapper.readTree(it) }
    response = objectMapper.convertValue(jsonNode, object : TypeReference<Map<String, Map<String, List<String>>>>() {})

    for ((key, value) in response) {
        value.get("ids")?.let {
            Activities(
                key = key,
                values = it)
        }?.let { ActivitiesList.add(it) }

请建议一种更快,更清洁的方法来做到这一点......

标签: kotlinamazon-s3objectmapperdata-classjsonnode

解决方案


推荐阅读