我正在尝试从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
panic: json: cannot unmarshal array into Go value of type main.Structure
该应用程序正在将JSON数组解组到结构。解组切片:
var decoded []interface{} err = json.Unmarshal(body, &decoded)
考虑解组为[] map [string]字符串或[] Tick,其中Tick是
type Tick struct { ID string Name string Symbol string Rank string ... and so on }