转换 Map映射<字符串,字符串>在科特林?

发布于 2025-01-11 21:44:01 字数 1083 浏览 0 评论 0 原文

我有一个数据库,我从中检索数据并将其存储在 Map 中。收藏。

在日志中打印时,它看起来像这样:

{-MxY_3dqegF7YmP-NEaE8={Factors={Percentage=52.89, Got Stuff=0, Happy=1}, Result=Ok, Probability=50}}

这看起来很好,在 Python 中我知道如何处理它,因为它就像一本字典,但问题是内部键和值是 Kotlin 中的 Any 类型对象。

我无法使用该类型并迭代键和列表。我只能将数据打印为字符串,但这就是我所知道的:

    val values = snapshot.getValue<Map<String, Any>>()
    Log.d("[CLIENT]", "Value is: $values")
    
    var keys = mutableListOf<String>()
    var data = mutableListOf<Any>()
    
    if (values != null) {
        for ((k, v) in values) {
            keys.add(k)
        }
    }
    
    val check = values?.get(keys[0])
    Log.d("[CLIENT]", "Key is: $check")

它将像这样打印 check

{Factors={Percentage=52.89, Got Stuff=0, Happy=1}, Result=Ok, Probability=50}

check 的类型为Any?,那么我该如何转换它或像地图一样使用它,或者任何其他更简单的方式,类似于 Python字典

I have a database where I retrieve data from and store it in a Map<String, Any> collection.

It looks something like this when printed in a Log:

{-MxY_3dqegF7YmP-NEaE8={Factors={Percentage=52.89, Got Stuff=0, Happy=1}, Result=Ok, Probability=50}}

This looks fine, in Python I would know how to go about it as it would be like a dictionary, but the problem is the inner keys and values are of this Any type object in Kotlin.

I am unable to work with that type and iterate through the keys and lists. I can only print the data as a String but that's all I know:

    val values = snapshot.getValue<Map<String, Any>>()
    Log.d("[CLIENT]", "Value is: $values")
    
    var keys = mutableListOf<String>()
    var data = mutableListOf<Any>()
    
    if (values != null) {
        for ((k, v) in values) {
            keys.add(k)
        }
    }
    
    val check = values?.get(keys[0])
    Log.d("[CLIENT]", "Key is: $check")

It would print check like this:

{Factors={Percentage=52.89, Got Stuff=0, Happy=1}, Result=Ok, Probability=50}

check is of type Any?, so how do I convert it or use it like a Map, or any other simpler way, similar to like maybe Python Dictionaries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深白境迁sunset 2025-01-18 21:44:01

回答将 Map 转换为 Map 的问题标题:

val values = hashMapOf<String, Any>("Foo" to 7, "Bar" to true)
val casted = values.map { e -> e.key to e.value.toString() }.toMap()

虽然我认为这不是您想要的,因为您似乎拥有一个Map>

Answering your question title of converting a Map<String, Any> to a Map<String, String>:

val values = hashMapOf<String, Any>("Foo" to 7, "Bar" to true)
val casted = values.map { e -> e.key to e.value.toString() }.toMap()

Although I don't think that's what you want, since you seem to have a Map<String, Map<String, Any>>.

泪痕残 2025-01-18 21:44:01

values.mapValues { it.values.toString() }

返回一个新映射,其中的条目具有此映射的键以及通过将转换函数应用于此映射中的每个条目而获得的值。

参考: https://kotlinlang.org/api/最新/jvm/stdlib/kotlin.collections/map-values.html

values.mapValues { it.values.toString() }

Returns a new map with entries having the keys of this map and the values obtained by applying the transform function to each entry in this Map.

Reference: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-values.html

奈何桥上唱咆哮 2025-01-18 21:44:01

想通了,简单的类型转换。抱歉,语言新手。

val check = values?.get(keys[0]) as Map<String, String>

这里唯一的问题是一个警告,说这是未经检查的演员,但我还没有弄清楚如何安全地做到这一点,如果你知道,请告诉我。目前它的工作原理是我知道数据类型。

按照 IDE 的建议,这也可以在没有警告的情况下工作:

val check = values?.get(keys[0]) as Map<*, *>

Figured it out, simple type casting. Apologies, new to the language.

val check = values?.get(keys[0]) as Map<String, String>

The only issue here is a warning that says it is an Unchecked cast, but I have yet to figure out how to do it safely, please let me know if you do. For now it works as I know the data types.

This also works without warnings, as recommended by the IDE:

val check = values?.get(keys[0]) as Map<*, *>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文