将map [interface {}] interface {}转换为map [string]字符串 [英] Convert map[interface {}]interface {} to map[string]string

查看:3482
本文介绍了将map [interface {}] interface {}转换为map [string]字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一个来源我不能影响我在地图中给出的数据,它以 map [interface {}] interface {}



我需要处理包含的数据,最好是以 map [string] string (内部的数据非常适合)。 >

我还需要从数据中生成密钥列表,因为这些不是事先知道的。



我可以在网上找到类似的问题或多或少地说,这是不可能的,但如果我的地图是 m fmt.Println(m) 显示数据,可读为 map [k0:v0 K1:v1 k2:v2 ...]



如何做fmt.Println能够做的事情?

解决方案

也许我误会了这个问题,但这个工作吗?

  m:= make(map [interface {}] interface {})
m [foo] =bar

m2:= make(map [string] string)

为键值:=范围m {
SW it键:= key。(type){
case string:
switch value:= value。(type){
case string:
m2 [key] = value
}
}
}


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?

解决方案

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
        }
    }
}

这篇关于将map [interface {}] interface {}转换为map [string]字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆