Go 从嵌套映射类型映射[字符串]接口{}获取值
我试图从嵌套映射中获取所有值,我不知道如何才能做到这一点Go 从嵌套映射类型映射[字符串]接口{}获取值,go,Go,我试图从嵌套映射中获取所有值,我不知道如何才能做到这一点 package main import "fmt" func main() { m := map[string]interface{}{ "date": "created", "clientName": "data.user.name", "address": map[string]interface{}{ "street": "x.addre
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?
}
}
如何从其他嵌套映射中获取值?您应该使用非平面强制转换来获取目标值
for i := range m {
nestedMap, ok := m[i].(map[string]interface{})
if ok {
// Do what you want
}
}
更多详细信息:您应该使用非平面铸造来达到目标值
for i := range m {
nestedMap, ok := m[i].(map[string]interface{})
if ok {
// Do what you want
}
}
更多细节:灵感来自@BayRinat
灵感来自@BayRinat
可能重复的可能重复的
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
}