Go Gin: how to marshall []byte to json without quotes

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

Go Gin: how to marshall []byte to json without quotes

问题

我尝试从我的测试应用程序返回 JSON。结果返回带有引号和转义字符。如何返回原始的 JSON?

type Row struct {
    Id    int    `json:"id"`
    Value string `json:"value"`
    Name  string `json:"name"`
}

func GetTestData() map[string]string {
    res := map[string]string{}

    // 数据库查询和其他逻辑

    for resultQuery.Next() {
        singleRow := Row{}
        errorScan := resultQuery.Scan(
            &singleRow.Id,
            &singleRow.Value,
            &singleRow.Name,
        )

        // 错误处理

        res[singleRow.Name] = singleRow.Value
    }

    return res
}

并调用此函数:

g.GET("/test", func(c *gin.Context) {
    out, _ := json.Marshal(services.GetTestData())
    c.JSON(200, gin.H{"output": out})
})

查询结果:

{
    "output": "{\"val one\":\"name one\",\"val two\":\"name two\"}"
}

我想要的结果是:

{
    "output": {"val one": "name one", "val two": "name two"}
}

我了解了 json.RawMessage,但不知道如何在这种情况下使用它。谢谢大家!

英文:

I try to return json from my test app. The result returns with quotes and escape characters. How to return raw json?

type Row struct {
	Id    int    `json:"id"`
	Value string `json:"value"`
	Name  string `json:"name"`
}

func GetTestData() map[string]string {
	res := map[string]string{}

//db query and other logic

     for resultQuery.Next() {
		singleRow := Row{}
		errorScan := resultQuery.Scan(
			&singleRow.Id,
			&singleRow.Value,
			&singleRow.Name,
		)

		//error scan here 

		res[singleRow.Name] = singleRow.Value
	}

    return res
}

And call this func

   g.GET("/test", func(c *gin.Context) {
		out, _ := json.Marshal(services.GetTestData())
		c.JSON(200, gin.H{"output": string(out)})
	})

Query result:

{
    "output": "{\"val one\":\"name one\",\"val two\":\"name two\"}"
}

I want to get result like

{
    "output": {"val one": "name one", "val two": "name two "}
}

I read about json.RawMessage but can't understand how to implemente it in this case.
Thanks everyone!

答案1

得分: 1

我认为这是你想要做的事情:

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/test", func(c *gin.Context) {
		res := GetTestData()
		c.JSON(http.StatusOK, gin.H{
			"output": res,
		})
	})
	r.Run()
}

type Row struct {
	Id    int    `json:"id"`
	Value string `json:"value"`
	Name  string `json:"name"`
}

func GetTestData() map[string]string {
	res := map[string]string{}

	//db query and other logic

	row1 := Row{
		Id:    1,
		Value: "1",
		Name:  "row_1",
	}

	row2 := Row{
		Id:    2,
		Value: "2",
		Name:  "row_2",
	}

	//error scan here

	res[row1.Name] = row1.Value
	res[row2.Name] = row2.Value

	return res
}

这是我在localhost:8080/test上的输出:

{
    "output": {
        "row_1": "1",
        "row_2": "2"
    }
}
英文:

I think it's what you are trying to do

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/test", func(c *gin.Context) {
		res := GetTestData()
		c.JSON(http.StatusOK, gin.H{
			"output": res,
		})
	})
	r.Run()
}

type Row struct {
	Id    int    `json:"id"`
	Value string `json:"value"`
	Name  string `json:"name"`
}

func GetTestData() map[string]string {
	res := map[string]string{}

	//db query and other logic

	row1 := Row{
		Id:    1,
		Value: "1",
		Name:  "row_1",
	}

	row2 := Row{
		Id:    2,
		Value: "2",
		Name:  "row_2",
	}

	//error scan here

	res[row1.Name] = row1.Value
	res[row2.Name] = row2.Value

	return res
}

this is what I have on localhost:8080/test

{
        "output": {
            "row_1": "1",
            "row_2": "2"
        }
    }

huangapple
  • 本文由 发表于 2022年9月28日 02:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/73872564.html
匿名

发表评论

匿名网友

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

确定