如何检索嵌套地图数据 "json.unmarshal()" 作为空接口

How to retrieve nested map data "json.unmarshal()" as empty interface

这似乎是一个简单的问题,但我还没有想出如何去做:我有一个嵌套的地图项,打印出来时如下所示:

fmt.Println("s3Info:", s3Info)

打印输出:

s3Info: [[map[s3Config:map[bucket:testbucket-data s3Key:runs/6033fd684304200011ef3bc5/init/03a78d21-446a-41bc-b4c1-eb66e04f45e2/52c8a076-f6c4-4180-8625-38ca52482628] size:158971 type:s3 varType:File]]

我想知道如何从对象 s3Info 中获取 buckets3Key 的值?

我尝试使用 s3Info.s3Config 访问 s3Config,但随后出现以下错误:

go/api_default_service_data_item.go:659:46: s3Info.s3Config undefined (type interface {} is interface with no methods)

我也试过用s3Info["s3Config"]访问s3Config,但是出现了如下错误:

go/api_default_service_data_item.go:660:46: invalid operation: s3Info["s3Config"] (type interface {} does not support indexing)

已添加: 该代码是处理来自 API 端点的查询响应的程序的一部分,以下是代码:

var runData map[string]interface{}

json.Unmarshal(body, &runData)

p := runData["p"].(map[string]interface{})
init := p["init"].(map[string]interface{})
outputs := init["outputs"].(map[string]interface{})
for key, s3Info := range outputs {
    // printout s3Info
    fmt.Println("s3Info:", s3Info)
    // check type
    switch c := s3Info.(type) {
        case string:
            fmt.Println("Key:", key, "=>", "s3Info:", s3Info)
        default:
            fmt.Printf("s3Info Type: %T\n", c)
    }
    // type assert to map
    s3Info := outputs[key].(map[string]interface{})
    fmt.Println("Key:", key, "=>", "s3Config:", s3Info["s3Config"])
}

打印结果如下:

s3Info: [map[s3Config:map[bucket:testbucket-data s3Key:runs/6033fd684304200011ef3bc5/init/03a78d21-446a-41bc-b4c1-eb66e04f45e2/52c8a076-f6c4-4180-8625-38ca52482628] size:158971 type:s3 varType:File]]
s3Info Type: []interface {}
interface conversion: interface {} is []interface {}, not map[string]interface {}

s3Infojson.Unmarshal() 解组为 interface{} 的数组,但不是映射。里面的内容可以通过type assertion[]interface{}.

找回

s3Config可以通过:

获得
for _, s := range s3Info.([]interface{}) {
  s3Config := s.(map[string]interface{})["s3Config"]
}

感谢@brits 的帮助 link: