将 map[interface{}]interface{} 转换为 map[string]string。

huangapple go评论68阅读模式
英文:

Convert map[interface {}]interface {} to map[string]string

问题

我无法直接影响数据源,该数据以 map[interface {}]interface {} 的形式传递给我。

我需要处理其中的数据,最好是将其转换为 map[string]string 的格式(其中的数据非常适合这种格式)。

我还需要生成数据中键的列表,因为这些键事先是未知的。

我在网上找到的大多数类似问题都表示这是不可能的,但是如果我的 map 是 mfmt.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 -->

huangapple
  • 本文由 发表于 2014年11月17日 23:20:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/26975880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定