dongqi7631 2014-12-20 01:46
浏览 35

从地图迭代并获取值

I have some JSON data that I've un-marshalled into a map called data_json. It contains several hundred items.

Using the following code I can successfully retrieve the value of "dn" for one of the items in the map, however I'm struggling how to iterate over the entire structure to get the value for "dn" for all items in the map.

objects := data_json["data"].([]interface{})
first := objects[0].(map[string]interface{})
fmt.Println(first["dn"])

I've tried this type of approach but I'm confused as to how I should construct keys and values.

for v, k := range keys {
fmt.Println("Key:", k, "Value:", m[k])
}
  • 写回答

1条回答 默认 最新

  • 「已注销」 2014-12-20 02:09
    关注

    If you means all items is objects, you will do that, like this:

    func printAllDataDn(data_json map[string]interface{}) {
        objects := data_json["data"].([]interface{})
        for _, v := range objects {
            item := v.(map[string]interface{})
            fmt.Println(item["dn"])
        }
    }
    
    评论

报告相同问题?