从嵌套的map类型map[string]interface{}中获取值。

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

get value form nested map type map[string]interface{}

问题

我正在尝试从嵌套的映射中获取所有的值,但我不知道该如何做到这一点。

package main

import "fmt"

func main() {
    m := map[string]interface{}{
        "date":       "created",
        "clientName": "data.user.name",
        "address": map[string]interface{}{
            "street": "x.address",
        },
        "other": map[string]interface{}{
            "google": map[string]interface{}{
                "value": map[string]interface{}{
                    "x": "y.address",
                },
            },
        },
        "new_address": map[string]interface{}{
            "address": "z.address",
        },
    }

    for i := range m {
        fmt.Println(m[i])
        // 如何从其他嵌套映射中获取值?
    }
}

如何从其他嵌套映射中获取值?

英文:

I'm trying to get all value from nested map and, I don't know how I can do that.

package main

import "fmt"

func main() {
	m := map[string]interface{}{
		"date":       "created",
		"clientName": "data.user.name",
		"address": map[string]interface{}{
			"street": "x.address",
		},
		"other": map[string]interface{}{
			"google": map[string]interface{}{
				"value": map[string]interface{}{
					"x": "y.address",
				},
			},
		},
		"new_address": map[string]interface{}{
			"address": "z.address",
		},
	}

	for i := range m {
		fmt.Println(m[i])
		// how I can get value from other nested map?
	}
}

how I can get value from other nested map?

答案1

得分: 7

你应该使用非恐慌式转换来针对目标值。

for i := range m {
	nestedMap, ok := m[i].(map[string]interface{})
	if ok {
		// 做你想做的事情
	}
}

更多详情请参考:https://golang.org/ref/spec#Type_assertions

英文:

You should use nonpanic casting to target value.

for i := range m {
	nestedMap, ok := m[i].(map[string]interface{})
	if ok {
		// Do what you want
	}
}

More details: https://golang.org/ref/spec#Type_assertions

答案2

得分: 4

受@BayRinat答案的启发

package main

import "fmt"

func main() {
    m := map[string]interface{}{
        "date":       "created",
        "clientName": "data.user.name",
        "address": map[string]interface{}{
            "street": "x.address",
        },
        "other": map[string]interface{}{
            "google": map[string]interface{}{
                "value": map[string]interface{}{
                    "x": "g.address",
                },
            },
        },
        "new_address": map[string]interface{}{
            "address": "z.address",
        },
        "key1": map[string]interface{}{
            "key2": map[string]interface{}{
                "key3": map[string]interface{}{
                    "key4": map[string]interface{}{
                        "key5": map[string]interface{}{
                            "key6": map[string]interface{}{
                                "key7": map[string]interface{}{
                                    "key": "enough",
                                },
                            },
                        },
                    },
                },
            },
        },
    }

    for i := range m {
        nestedMap, ok := m[i].(map[string]interface{})
        if ok {
            fmt.Println("Key:", i)
            fmt.Println("Value:", getValNestedMap(nestedMap))
        } else {
            fmt.Println("Key:", i)
            fmt.Println("Value:", m[i])
        }
    }

}

func getValNestedMap(m map[string]interface{}) interface{} {
    for i := range m {
        nestedMap, ok := m[i].(map[string]interface{})
        if ok {
            return getValNestedMap(nestedMap)
        }
        return m[i]
    }

    return nil
}

Go Playground

参考链接:答案

英文:

Inspired by @BayRinat answer

package main
import "fmt"
func main() {
m := map[string]interface{}{
"date":       "created",
"clientName": "data.user.name",
"address": map[string]interface{}{
"street": "x.address",
},
"other": map[string]interface{}{
"google": map[string]interface{}{
"value": map[string]interface{}{
"x": "g.address",
},
},
},
"new_address": map[string]interface{}{
"address": "z.address",
},
"key1": map[string]interface{}{
"key2": map[string]interface{}{
"key3": map[string]interface{}{
"key4": map[string]interface{}{
"key5": map[string]interface{}{
"key6": map[string]interface{}{
"key7": map[string]interface{}{
"key": "enough",
},
},
},
},
},
},
},
}
for i := range m {
nestedMap, ok := m[i].(map[string]interface{})
if ok {
fmt.Println("Key:", i)
fmt.Println("Value:", getValNestedMap(nestedMap))
} else {
fmt.Println("Key:", i)
fmt.Println("Value:", m[i])
}
}
}

func getValNestedMap(m map[string]interface{}) interface{} {
for i := range m {
nestedMap, ok := m[i].(map[string]interface{})
if ok {
return getValNestedMap(nestedMap)
}
return m[i]
}
return nil
}

Go Playground

huangapple
  • 本文由 发表于 2017年7月7日 20:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/44971026.html
匿名

发表评论

匿名网友

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

确定