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

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

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()函数。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. mapInterface := make(map[interface{}]interface{})
  7. mapString := make(map[string]string)
  8. mapInterface["k1"] = 1
  9. mapInterface[3] = "hello"
  10. mapInterface["world"] = 1.05
  11. for key, value := range mapInterface {
  12. strKey := fmt.Sprintf("%v", key)
  13. strValue := fmt.Sprintf("%v", value)
  14. mapString[strKey] = strValue
  15. }
  16. fmt.Printf("%#v", mapString)
  17. }

以上代码演示了如何使用fmt.Sprintf()函数将未知接口类型转换为字符串类型,并将其存储在mapString中。

英文:

A secure way to process unknown interfaces, just use fmt.Sprintf()

https://play.golang.org/p/gOiyD4KpQGz

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. mapInterface := make(map[interface{}]interface{})
  7. mapString := make(map[string]string)
  8. mapInterface["k1"] = 1
  9. mapInterface[3] = "hello"
  10. mapInterface["world"] = 1.05
  11. for key, value := range mapInterface {
  12. strKey := fmt.Sprintf("%v", key)
  13. strValue := fmt.Sprintf("%v", value)
  14. mapString[strKey] = strValue
  15. }
  16. fmt.Printf("%#v", mapString)
  17. }

答案2

得分: 18

也许我误解了问题,但这段代码可以工作吗?

  1. m := make(map[interface{}]interface{})
  2. m["foo"] = "bar"
  3. m2 := make(map[string]string)
  4. for key, value := range m {
  5. switch key := key.(type) {
  6. case string:
  7. switch value := value.(type) {
  8. case string:
  9. m2[key] = value
  10. }
  11. }
  12. }

这段代码的作用是将m中的键值对复制到m2中,但只复制那些键和值都是字符串类型的键值对。

英文:

Perhaps I misunderstand the question, but would this work?

  1. m := make(map[interface{}]interface{})
  2. m["foo"] = "bar"
  3. m2 := make(map[string]string)
  4. for key, value := range m {
  5. switch key := key.(type) {
  6. case string:
  7. switch value := value.(type) {
  8. case string:
  9. m2[key] = value
  10. }
  11. }
  12. }

答案3

得分: 0

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. // data is map[string]interface{}
  2. form := make(map[string]string)
  3. for k, v := range data {
  4. form[k] = v.(string)
  5. }

<!-- end snippet -->

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-html -->

  1. // data 是 map[string]interface{}
  2. form := make(map[string]string)
  3. for k, v := range data {
  4. form[k] = v.(string)
  5. }

<!-- 结束代码片段 -->

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. // data is map[string]interface{}
  2. form := make(map[string]string)
  3. for k, v := range data {
  4. form[k] = v.(string)
  5. }

<!-- 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:

确定