英文:
golang querying database using Gorm and returning json in http response
问题
我是你的中文翻译助手,以下是你提供的代码的翻译:
我刚开始学习Go语言,并且正在使用Gorm来查询我的PostgreSQL数据库,但是在以字典格式返回数据时遇到了问题,其中宝可梦的类型作为键,对应一个包含该类型所有宝可梦的数组。
> json: 无法将对象解组为类型为[]models.Pokemon的Go值
以下是我的代码:
type Pokemon struct {
Name string `db:"name"`
Type string `db:"type"`
}
pokemonTypes := [6]string{
"fire",
"electric",
"water",
"grass",
}
var retData struct {
Poke []Pokemon
}
m := make(map[string][]Pokemon)
for _, t := range pokemonTypes {
pokemon := DB.Where(&Pokemon{Type: t}).Find(&retData.Poke)
p, _ := json.Marshal(pokemon)
err = json.Unmarshal(p, &retData.Poke) // 在这里出错
if err != nil {
fmt.Println(err)
}
m[category] = retData.Poke
}
data, _ := json.Marshal(m)
w.Write(data) // HTTP响应
我在数据库中有以下数据:
name | type
----------------------
pikachu | electric
charmander | fire
blaziken | fire
venusaur | grass
treeko | grass
squirtle | water
我希望以以下JSON格式返回数据:
{
"electric": [
{"name": "pikachu", "type": "electric"},
],
"fire": [
{"name": "charmander", "type": "fire"},
{"name": "blaziken", "type": "fire"}
],
"grass": [
{"name": "venusaur", "type": "grass"},
{"name": "treeko", "type": "grass"},
],
"water": [
{"name": "squirtle", "type": "water"},
]
}
英文:
I'm new to Go and am using Gorm to query my postgres database but am having trouble returning my data in a dictionary format, where the pokemon's type serves as a key to an array of all the pokemon of that type
> json: cannot unmarshal object into Go value of type []models.Pokemon
here's my code:
type Pokemon struct {
Name string `db:"name"`
Type string `db:"type"`
}
pokemonTypes := [6]string{
"fire",
"electric",
"water",
"grass",
}
var retData struct {
Poke []Pokemon
}
m := make(map[string][]Pokemon)
for _, t := range pokemonTypes {
pokemon := DB.Where(&Pokemon{Type: t}).Find(&retData.Poke)
p, _ := json.Marshal(pokemon)
err = json.Unmarshal(p, &retData.Poke) // getting error here
if err != nil {
fmt.Println(err)
}
m[category] = retData.Poke
}
data, _ := json.Marshal(m)
w.Write(data) // http repsonse
I have this in my database
name | type
----------------------
pikachu | electric
charmander | fire
blaziken | fire
venusaur | grass
treeko | grass
squirtle | water
I want to return data in this json format
{
“electric”: [
{"name": "pikachu", "type": "electric"},
],
"fire": [
{"name": "charmander", "type": "fire"},
{"name": "blaziken", "type": "fire"}
],
"grass": [
{"name": "venusaur", "type": "grass"},
{"name": "treeko", "type": "grass"},
],
"water": [
{"name": "squirtle", "type": "water"},
]
}
答案1
得分: 2
DB.Where(&Pokemon{Type: t}).Find(&retData.Poke)
实际上返回了 *db
指针,你可以使用它来链式调用其他方法。
当你执行 .Find(&retData.Poke)
时,已经将 postgre 行反序列化到了你的结构体切片中。因此,pokemon
实际上不是你想象中的那样。
现在只剩下将 .Find()
与 .Error()
链接起来,以便返回并检查查询中的任何错误。就像这样:
for _, t := range pokemonTypes {
err := DB.Where(&Pokemon{Type: t}).Find(&retData.Poke).Error()
if err != nil {
fmt.Println(err)
return
}
p, _ := json.Marshal(retData.Poke)
err = json.Unmarshal(p, &retData.Poke)
if err != nil {
fmt.Println(err)
return
}
m[category] = retData.Poke
}
希望对你有所帮助!
英文:
DB.Where(&Pokemon{Type: t}).Find(&retData.Poke)
esentially returns back the *db
pointer which you can use to chain further methods.
You're already deserializing postgre rows into your struct slice when you do .Find(&retData.Poke)
. Thus, pokemon
isn't actually what you think it is.
The only thing left now is to chain .Find()
with a .Error()
so that you can return and check any error in your query. Just like this :
for _, t := range pokemonTypes {
err := DB.Where(&Pokemon{Type: t}).Find(&retData.Poke).Error()
if err != nil {
fmt.Println(err)
return
}
p, _ := json.Marshal(retData.Poke)
err = json.Unmarshal(p, &retData.Poke)
if err != nil {
fmt.Println(err)
return
}
m[category] = retData.Poke
}
Hope it helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论