【问题标题】:I am trying to print JSON in go我正在尝试在 go 中打印 JSON
【发布时间】: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_URLAPI_VERSION。此外,请接受之前的建议并停止忽略错误。他们在那里是有原因的。
  • 非常感谢您的帮助,我实际上发现我不应该提供指向字符串的 http spotify api 链接。它现在工作了,但问题是它正在返回一个 JSON 文件,这是我第一次解析 JSON,但它不工作你知道如何在解析它之前打印 JSON 作为第一步吗?我会更新我的代码

标签: json api go spotify


【解决方案1】:

如果您将大量数字作为 JSON 正文而不是文本加载,那么它可能是字节数组而不是字符串。要解决此问题,请转换为字符串

response, _ := spot.Get(s, nil)
    fmt.Println(string(response))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2016-01-18
    • 2019-11-13
    • 1970-01-01
    • 2015-08-09
    • 2017-05-26
    • 2015-06-27
    • 1970-01-01
    相关资源
    最近更新 更多