【发布时间】:2018-12-02 13:54:18
【问题描述】:
如何打印 JSON?当我打印时,我不断在数组中获取随机数有没有办法打印 JSON 而不是解析它? 这是main.go中的函数
func sendpostget(word string) {
// Create a new spotify object
spot := spotify.New("number", "number")
// Authorize against Spotify first
authorized, _ := spot.Authorize()
if authorized {
// If we ere able to authorize then Get a simple album
// s := join("artists/", phone)
s := "artists/xxxx/albums?market=ES&limit=2"
fmt.Println(s)
response, _ := spot.Get(s, nil)
fmt.Println(response)
}
// Parse response to a JSON Object and
}
这是我正在使用的结构(此 get 方法将从 spotify API 检索艺术家的专辑)
type Albums struct {
Albums []Album `json:"users"`
}
type Album struct {
AlbumType string `json:"album_type"`
Href string `json:"href"`
ID string `json:"id"`
}
【问题讨论】:
-
先打印
authorized, _ := spot.Authorize()的错误而不是扔掉它,它可能会告诉你哪里出了问题。 -
您在 URL 中提供的信息过多。它真的应该是一个端点。如果您阅读包的代码,您会看到他们采用您提供的端点并在其前面加上
BASE_URL和API_VERSION。此外,请接受之前的建议并停止忽略错误。他们在那里是有原因的。 -
非常感谢您的帮助,我实际上发现我不应该提供指向字符串的 http spotify api 链接。它现在工作了,但问题是它正在返回一个 JSON 文件,这是我第一次解析 JSON,但它不工作你知道如何在解析它之前打印 JSON 作为第一步吗?我会更新我的代码