将Gorm输出转换为JSON。

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

Gorm output to json

问题

我正在尝试将 SQL 输出(GORP)转换为 JSON。我正在使用 gorp 和 mySql。

以下是我选择的代码:

type Mane struct {
    ManeId   string `db:"mane_id"`
    Manetana string `db:"manetana"`
    Yajamana string `db:"yajamana"`
}

var manegalu []Mane
_, err = dbmap.Select(&manegalu, "SELECT mane_id, manetana, yajamana FROM kd_mane")

// 选项1:不起作用:数组中只有数字,不是实际的 JSON
a, err := json.Marshal(manegalu)
mt.Fprint(w, string(a))

// 选项2:不起作用:数组中只有数字,不是实际的 JSON
for _, p := range manegalu {
    a, err := json.Marshal(p)
    fmt.Fprint(w, string(a))
}

我期望的输出如下:

{"mane_id":3323, "manetana":"ABC", "yajamana":"hgy"},{"mane_id":2323, "manetana":"ADFC", "yajamana":"FDER"},{"mane_id":12343, "manetana":"GDSC", "yajamana":"hFDEy"}

请问我做错了什么?我知道为什么选项1不起作用。但是根据 https://gobyexample.com/json,选项2对我来说似乎是正确的。

英文:

I am trying to convert a SQL Output (GORP) to JSON. I am using gorp with mySql.

Here is my selected code

type Mane struct {
	ManeId   string `db:"mane_id"`
	Manetana string `db:"manetana"`
	Yajamana string `db:"yajamana"`
}

var manegalu []Mane
_, err = dbmap.Select(&manegalu, "SELECT mane_id, manetana, yajamana FROM kd_mane")

//Option 1: Not working: Array of numbers. Not the actual json
a, err := json.Marshal(manegalu)
mt.Fprint(w, string(a))

//Option 2: Not working: Array of numbers. Not the actual json
for _, p := range manegalu {
	a, err := json.Marshal(p)
	fmt.Fprint(w, string(a))
}

I am expecting and output like this

{"mane_id":3323, "manetana":"ABC", "yajamana":"hgy"},{"mane_id":2323, "manetana":"ADFC", "yajamana":"FDER"},{"mane_id":12343, "manetana":"GDSC", "yajamana":"hFDEy"} 

Can you please let me know what am I doing wrong? I understand that why option 1 does not work. But option 2 seems good for me as per https://gobyexample.com/json

答案1

得分: 5

明白了。

a, err := json.Marshal(manegalu) //获取 JSON 字节数组
n := len(a)   //获取字节数组的长度
s := string(a[:n]) //转换为字符串
fmt.Fprint(w, s) //写入响应中
英文:

Got it done.

a, err := json.Marshal(manegalu) //get json byte array
n := len(a)   //Find the length of the byte array
s := string(a[:n]) //convert to string
fmt.Fprint(w, s) //write to response

答案2

得分: 2

json.Marshal() 返回两个值,[]byteerror。在你的示例中,你只赋值了第一个值。请参考 http://golang.org/pkg/encoding/json/#Marshal

a, err := json.Marshal(manegalu)

编辑:你需要将字节数组转换为字符串才能在 fmt.Fprint 中使用。在你的情况下,w 是一个 io.Writer 接口,所以你也可以使用:

w.Write(a)

来打印结果,而无需导入 fmt 包。请参考 http://golang.org/pkg/io/#Writer

英文:

json.Marshal() returns two values, []byte and error. You're only assigning the first in your example. See http://golang.org/pkg/encoding/json/#Marshal.

a, err := json.Marshal(manegalu)

Edit: You'll need to convert the byte array to a string to use it with fmt.Fprint. In your case, w is an io.Writer interface, so you can also use:

w.Write(a)

to print the results without importing the fmt package. See http://golang.org/pkg/io/#Writer

huangapple
  • 本文由 发表于 2014年11月26日 02:10:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/27133761.html
匿名

发表评论

匿名网友

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

确定