Go Gin converting json response to base64

huangapple go评论120阅读模式
英文:

Go Gin converting json response to base64

问题

我正在尝试将数据库查询数据作为JSON响应发送。以下是我的控制器代码:

  1. import (
  2. "fmt"
  3. "github.com/json-iterator/go"
  4. "log"
  5. )
  6. func GetNewsPapers() []byte {
  7. db := GetDB()
  8. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  9. rows, err := db.Queryx(`SELECT title, language, ranking, slug, search_term, logo_url FROM public.news_newspaper`)
  10. if err != nil {
  11. log.Println(err)
  12. }
  13. defer rows.Close()
  14. tableData := make([]map[string]interface{}, 0)
  15. for rows.Next() {
  16. entry := make(map[string]interface{})
  17. err := rows.MapScan(entry)
  18. if err != nil {
  19. log.Println(err)
  20. }
  21. tableData = append(tableData, entry)
  22. }
  23. jsonData, _ := json.Marshal(tableData)
  24. fmt.Println(string(jsonData)) // 打印预期的JSON
  25. err = rows.Err()
  26. if err != nil {
  27. panic(err)
  28. }
  29. return jsonData
  30. }
  31. func (n *NewsPaperController) GetList(c *gin.Context) {
  32. value := database.GetNewsPapers()
  33. c.JSON(http.StatusOK, value)
  34. }

问题是我得到的响应是Base64字符串,而不是我期望的JSON对象。如果我像下面这样将value转换为字符串,我可以得到可读的值。

  1. c.JSON(http.StatusOK, string(value))

但整个响应被编码为字符串,像这样:

  1. "[{\"language\":\"en\",\"logo_url\":\"..\",\"ranking\":2,\"search_term\":\"..\",\"slug\":\"..\",\"title\":\"....\"}]"

我该如何获得以下JSON响应:

  1. [{"language":"en","logo_url":"..","ranking":2,"search_term":"..","slug":"..","title":".."}]
英文:

I am trying to send db query data as json response . Here is my controller :

  1. import (
  2. "fmt"
  3. "github.com/json-iterator/go"
  4. "log"
  5. )
  6. func GetNewsPapers() []byte{
  7. db := GetDB()
  8. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  9. rows, err := db.Queryx(`SELECT title, language, ranking, slug, search_term, logo_url FROM public.news_newspaper`)
  10. if err != nil {
  11. log.Println(err)
  12. }
  13. defer rows.Close()
  14. tableData := make([]map[string]interface{}, 0)
  15. for rows.Next() {
  16. entry := make(map[string]interface{})
  17. err := rows.MapScan(entry)
  18. if err != nil {
  19. log.Println(err)
  20. }
  21. tableData = append(tableData, entry)
  22. }
  23. jsonData, _ := json.Marshal(tableData)
  24. fmt.Println(string(jsonData)) // printing expected json
  25. err = rows.Err()
  26. if err != nil {
  27. panic(err)
  28. }
  29. return jsonData
  30. }

and

  1. func (n *NewsPaperController) GetList(c *gin.Context) {
  2. value := database.GetNewsPapers()
  3. c.JSON(http.StatusOK, value)
  4. }

Problem is I am getting base64 string as response not the json object I am expecting. If I convert value to string like below, I get human readable values .

  1. c.JSON(http.StatusOK, string(value))

But whole response encoded in string like this:

  1. "[{\"language\":\"en\",\"logo_url\":\"..\",\"ranking\":2,\"search_term\":\"..\",\"slug\":\"..\",\"title\":\"....\"}]

How do I get json response like below:

  1. [{"language":"en","logo_url":"..","ranking":2,"search_term":"..","slug":"..","title":".."} ]

答案1

得分: 1

  1. // func (c *Context) JSON(code int, obj interface{})
  2. //
  3. // JSON将给定的结构体序列化为JSON,并将Content-Type设置为"application/json"。
  4. //
  5. // 在c.JSON()中使用`tableData`,无需在使用之前进行解组。
  6. func GetNewsPapers() []map[string]interface{} {
  7. // 你现有的代码
  8. return tableData
  9. }
  10. func (n *NewsPaperController) GetList(c *gin.Context) {
  11. value := database.GetNewsPapers()
  12. c.JSON(http.StatusOK, value)
  13. }
  14. // 使用`%#v`可以查看值的Go语法表示,其中也包含转义字符
  15. fmt.Printf("%#v", string(jsonData))

希望这个翻译对你有帮助!如果你有任何其他问题,请随时问。

英文:

> func (c *Context) JSON(code int, obj interface{})
>
>JSON serializes the
> given struct as JSON into the response body. It also sets the
> Content-Type as "application/json".

c.JSON() serialize as JSON you don't need to unmarshal before use it. Use tableData in c.JSON()

  1. func GetNewsPapers() []map[string]interface{}{
  2. // your existing code
  3. return tableData
  4. }
  5. func (n *NewsPaperController) GetList(c *gin.Context) {
  6. value := database.GetNewsPapers()
  7. c.JSON(http.StatusOK, value)
  8. }

And using %#v you can see Go-syntax representation of the value where you will found the escape character also

  1. fmt.Printf("%#v", string(jsonData))

huangapple
  • 本文由 发表于 2020年5月17日 12:47:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61847115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定