我正处于婴儿期,试图把我的思想集中在围棋上.目前,我正在模拟一个API请求,该请求返回一个包含对象数组的JSON格式字符串.我试图找出最合适的方法来遍历每条记录并访问每个字段.最终,每个字段都将写入ExcelElectron 表格,但现在我只想打印每个字段的键&价值

以下是我所拥有的(我会在围棋场上提供,但不支持HTTP请求):

    response, err := http.Get("http://go-proto.robwilkerson.org/demo.json")
    failOnError(err, "Uh oh")
    defer response.Body.Close()

    var view []interface{}
    json.NewDecoder(response.Body).Decode(&view)
    log.Printf(" [x] Pulled JSON: %s", view)
    for _, record := range view {
        log.Printf(" [===>] Record: %s", record)

        for key, val := range record {
            log.Printf(" [========>] %s = %s", key, val)
        }
    }

在try 迭代保存每条记录属性的map的嵌套循环之前,一切都运行得很好:

cannot range over record (type interface {})

我想我有两个问题:

  1. 嵌套循环是访问每条记录的每个属性的最有效/最高效的方式吗?
  2. 我需要做些什么才能解决该错误?

UPDATE

当我将解码成view变量的数据转储时,这是记录的结果:

[
    map[id:ef14912f-8031-42b3-8c50-7aa612287534 avatar:http://placehold.it/32x32 name:Vilma Hobbs email:vilmahobbs@exiand.com phone:+1 (886) 549-3522 address:471 Dahill Road, Jacksonwald, Alabama, 6026] 
    map[id:1b7bf182-2482-4b8b-8210-9dc9ee51069e avatar:http://placehold.it/32x32 name:Anne Dalton email:annedalton@exiand.com phone:+1 (994) 583-2947 address:660 Macdougal Street, Ticonderoga, Alaska, 7942] 
    map[id:f8027852-f52e-4bbb-bc9d-fb5e34929b40 avatar:http://placehold.it/32x32 name:Amie Ray email:amieray@exiand.com phone:+1 (853) 508-3649 address:878 Kane Street, Derwood, Minnesota, 3826] 
    map[id:b9842ab7-5053-48b4-a991-f5c63af8fb7e avatar:http://placehold.it/32x32 name:Hope Benton email:hopebenton@exiand.com phone:+1 (938) 542-2232 address:396 Osborn Street, Rowe, Massachusetts, 702] 
    map[id:8f9f6d8d-d14e-4ddc-acb2-eb96d3c3d7a8 avatar:http://placehold.it/32x32 name:Janine Kidd email:janinekidd@exiand.com phone:+1 (877) 474-2633 address:173 Manhattan Court, Hall, Virginia, 7376] 
    map[avatar:http://placehold.it/32x32 name:Kristen Yang email:kristenyang@exiand.com phone:+1 (862) 469-3446 address:203 Doughty Street, Westmoreland, Rhode Island, 849 id:210a6ae6-8227-4f26-a47c-448c400f26e9] 
    map[name:Murray Sosa email:murraysosa@exiand.com phone:+1 (814) 549-2536 address:811 Kingsland Avenue, Shepardsville, Hawaii, 9117 id:05f4f6af-19af-4b76-9aa8-9352281cf7d9 avatar:http://placehold.it/32x32] 
    map[phone:+1 (918) 476-2264 address:176 Underhill Avenue, Ebro, Washington, 8099 id:9d21268d-067c-4b16-a6cd-42d184c4c059 avatar:http://placehold.it/32x32 name:Paige Stanton email:paigestanton@exiand.com] 
    map[id:9946c19f-c226-4ff3-b578-57a249f8d0c7 avatar:http://placehold.it/32x32 name:Melisa Oconnor email:melisaoconnor@exiand.com phone:+1 (908) 429-3915 address:768 Wallabout Street, Cotopaxi, Florida, 3388] 
    map[id:ee0b82cb-4990-428b-ba36-088287a404ba avatar:http://placehold.it/32x32 name:Monique Poole email:moniquepoole@exiand.com phone:+1 (925) 564-2690 address:232 Hubbard Place, Islandia, Michigan, 3722]
]

推荐答案

当类型未声明时,json包将解码成的默认值为:

bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[]interface{}, for JSON arrays
map[string]interface{}, for JSON objects
nil for JSON null

由于每个record(在您的示例中)都是一个json对象,因此可以将每个record断言为map[string]interface{},如下所示:

for _, record := range view {
    log.Printf(" [===>] Record: %s", record)

    if rec, ok := record.(map[string]interface{}); ok {
        for key, val := range rec {
            log.Printf(" [========>] %s = %s", key, val)
        }
    } else {
        fmt.Printf("record not a map[string]interface{}: %v\n", record)
    }
}

Go相关问答推荐

带有一个新变量的Go冒号等于运算符

允许在 struct 中使用复合作为函数参数

当我有外键时,如何使用 GORM 创建数据库的新条目

Go:如何在不加载正文的情况下创建 http 代理通行证

用 fork 替换 Go 依赖:...用于两个不同的模块路径

如何用mpb创建两行进度条?

加密/椭圆:try 在无效点上进行操作

如何判断范围内的字段?

go - 仅在函数即将返回错误时清理资源

regex.ReplaceAll 但如果替换则添加相同数量的字符

Wire google Inject with multi return from provider 函数

使用 go.work 文件在多个测试文件上运行 go test 命令

如何修改go gin的默认端口?我的 8080 端口正在使用中

Go 泛型:自引用接口约束

为什么 go.mod 中的所有依赖都是间接的?

函数的递归调用以 goroutine 和惯用方式开始,以在所有工作 goroutine 完成时继续调用者

go routine 和接收错误或成功的通道

在 VSCode 中使用命令行参数调试 Go 测试

Go/Golang:如何从 big.Float 中提取最低有效数字?

手动下载并放置一个 golang mod 文件