【问题标题】:get value form nested map type map[string]interface{}从嵌套映射类型 map[string]interface{} 中获取值
【发布时间】:2017-12-11 18:24:00
【问题描述】:

我正在尝试从嵌套地图中获取所有价值,但我不知道该怎么做。

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

如何从其他嵌套地图中获取价值?

【问题讨论】:

标签: go


【解决方案1】:

您应该使用非恐慌强制转换为目标值。

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

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

【讨论】:

    【解决方案2】:

    灵感来自@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

    【讨论】:

      猜你喜欢
      • 2015-05-02
      • 2023-04-01
      • 2013-12-05
      • 1970-01-01
      • 2021-06-22
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多