英文:
Convert map[interface {}]interface {} to map[string]string
问题
我无法直接影响数据源,该数据以 map[interface {}]interface {}
的形式传递给我。
我需要处理其中的数据,最好是将其转换为 map[string]string
的格式(其中的数据非常适合这种格式)。
我还需要生成数据中键的列表,因为这些键事先是未知的。
我在网上找到的大多数类似问题都表示这是不可能的,但是如果我的 map 是 m
,fmt.Println(m)
可以显示数据,格式为 map[k0:v0 K1:v1 k2:v2 ... ]
。
我该如何做到像 fmt.Println
一样的效果?
英文:
From a source I cannot influence I am given data in a map, which arrives as map[interface {}]interface {}
.
I need to process the contained data, preferably as map[string]string
(the data within is perfectly suitable for that).
I need to generate a list of the keys from the data as well, as those are not known beforehand.
Most similar questions I could find on the web say more or less, that this is impossible, but if my map is m
, fmt.Println(m)
shows the data is there, readable as map[k0:v0 K1:v1 k2:v2 ... ]
.
How can I do what fmt.Println is able to do?
答案1
得分: 30
一种安全的处理未知接口的方法是使用fmt.Sprintf()
函数。
package main
import (
"fmt"
)
func main() {
mapInterface := make(map[interface{}]interface{})
mapString := make(map[string]string)
mapInterface["k1"] = 1
mapInterface[3] = "hello"
mapInterface["world"] = 1.05
for key, value := range mapInterface {
strKey := fmt.Sprintf("%v", key)
strValue := fmt.Sprintf("%v", value)
mapString[strKey] = strValue
}
fmt.Printf("%#v", mapString)
}
以上代码演示了如何使用fmt.Sprintf()
函数将未知接口类型转换为字符串类型,并将其存储在mapString
中。
英文:
A secure way to process unknown interfaces, just use fmt.Sprintf()
https://play.golang.org/p/gOiyD4KpQGz
package main
import (
"fmt"
)
func main() {
mapInterface := make(map[interface{}]interface{})
mapString := make(map[string]string)
mapInterface["k1"] = 1
mapInterface[3] = "hello"
mapInterface["world"] = 1.05
for key, value := range mapInterface {
strKey := fmt.Sprintf("%v", key)
strValue := fmt.Sprintf("%v", value)
mapString[strKey] = strValue
}
fmt.Printf("%#v", mapString)
}
答案2
得分: 18
也许我误解了问题,但这段代码可以工作吗?
m := make(map[interface{}]interface{})
m["foo"] = "bar"
m2 := make(map[string]string)
for key, value := range m {
switch key := key.(type) {
case string:
switch value := value.(type) {
case string:
m2[key] = value
}
}
}
这段代码的作用是将m
中的键值对复制到m2
中,但只复制那些键和值都是字符串类型的键值对。
英文:
Perhaps I misunderstand the question, but would this work?
m := make(map[interface{}]interface{})
m["foo"] = "bar"
m2 := make(map[string]string)
for key, value := range m {
switch key := key.(type) {
case string:
switch value := value.(type) {
case string:
m2[key] = value
}
}
}
答案3
得分: 0
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
// data is map[string]interface{}
form := make(map[string]string)
for k, v := range data {
form[k] = v.(string)
}
<!-- end snippet -->
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言:lang-html -->
// data 是 map[string]interface{}
form := make(map[string]string)
for k, v := range data {
form[k] = v.(string)
}
<!-- 结束代码片段 -->
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
// data is map[string]interface{}
form := make(map[string]string)
for k, v := range data {
form[k] = v.(string)
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论