你想达到什么目的?
我正在尝试解析来自 json api 的数据。
粘贴显示问题的代码部分。
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Structure struct {
stuff []interface{}
}
func main() {
url := "https://api.coinmarketcap.com/v1/ticker/?start=0&limit=100"
response, err := http.Get(url)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
decoded := &Structure{}
fmt.Println(url)
err = json.Unmarshal(body, decoded)
if err != nil {
panic(err)
}
fmt.Println(decoded)
}
你期望结果是什么?
我希望代码返回一个接口对象列表。
你得到的实际结果是什么?
我得到一个错误: panic: json: cannot unmarshal array into Go value of type main.Structure
原文由 Peter S 发布,翻译遵循 CC BY-SA 4.0 许可协议
应用程序正在将 JSON 数组解组为结构。解组为一个片段:
考虑解组到特定于响应数据的结构片段: