你好,可以使用 json.Unmarshal
方法将接口返回的数据转换成 map interface。示例代码如下:
import (
"encoding/json"
"fmt"
"net/http"
)
func main() {
resp, err := http.Get("https://example.com/api")
if err != nil {
panic(err)
}
defer resp.Body.Close()
var data map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
panic(err)
}
fmt.Println(data)
}
在上面的示例中,我们首先使用 http.Get
方法获取接口返回的数据,然后使用 json.NewDecoder
将数据解析成 map interface 类型。最后打印出来以验证是否成功解析。
注意:这里假设接口返回的是 JSON 格式数据,如果是其他格式需要相应调整解析方法。另外,map interface 可能会导致类型不安全和运行时错误,请谨慎使用。