英文:
Golang: Returning array at http response in Gin framework doesn't work
问题
我正在尝试将数组[]Employee作为响应返回。它的长度为2。但是Postman显示响应体为空。
我在Google上找到了关于Golang在内部绑定结构方面存在一些问题的信息。
更新:使用MarshalJSON覆盖的代码。这是问题所在。
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
type TypeName struct {
Name string `json:"name" binding:"required"`
}
func (p TypeName) MarshalJSON() ([]byte, error) {
return []byte("Joe"), nil
}
type People struct {
Height int32 `json:"height" binding:"required"`
Weight int32 `json:"weight" binding:"required"`
TypeName
}
type Employee struct {
Salary int `json:"salary"`
People
}
func main() {
r := gin.Default()
r.GET("/employees", func(c *gin.Context) {
employeeArray := []Employee{
{
Salary: 10000,
People: People{
Height: 170,
Weight: 80,
},
},
{
Salary: 20000,
People: People{
Height: 175,
Weight: 80,
},
},
}
c.JSON(http.StatusOK, employeeArray)
})
_ = r.Run()
}
希望这可以帮助到你。
英文:
I am trying to return array []Employee as a response. Is has len 2. But postman shows that responses body is empty.
type People struct {
Height int32 `json:"height" binding:"required"`
Weight int32 `json:"weight" binding:"required"`
}
type Employee struct {
Salary int `json:"salary"`
People
}
c.JSON(http.StatusOK, employeeArray)
I found in google that golang has some porblems with inner Binding structures.
UPD: code with MarshalJSON overriding. Here is the porblem
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
type TypeName struct {
Name string `json:"name" binding:"required"`
}
func (p TypeName) MarshalJSON() ([]byte, error) {
return []byte("Joe"), nil
}
type People struct {
Height int32 `json:"height" binding:"required"`
Weight int32 `json:"weight" binding:"required"`
TypeName
}
type Employee struct {
Salary int `json:"salary"`
People
}
func main() {
r := gin.Default()
r.GET("/employees", func(c *gin.Context) {
employeeArray := []Employee{
{
Salary: 10000,
People: People{
Height: 170,
Weight: 80,
},
},
{
Salary: 20000,
People: People{
Height: 175,
Weight: 80,
},
},
}
c.JSON(http.StatusOK, employeeArray)
})
_ = r.Run()
}
答案1
得分: 3
func (p TypeName) MarshalJSON() ([]byte, error) {
return []byte(`"Joe"`), nil
}
[]byte("Joe")
不是一个有效的 JSON。它应该加上引号。如果你注意到 gin 的日志,你应该会看到一个错误消息:Error #01: json: error calling MarshalJSON for type main.Employee: invalid character 'J' looking for beginning of value
。这个应该可以工作:
func (p TypeName) MarshalJSON() ([]byte, error) {
return []byte(`"Joe"`), nil
}
这里是一个关于 time.Time
的示例:
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type Time struct {
StandardTime time.Time
}
func (p Time) MarshalJSON() ([]byte, error) {
timeString := p.StandardTime.Format(time.RFC3339)
return json.Marshal(timeString)
}
func main() {
r := gin.Default()
r.GET("/time", func(c *gin.Context) {
timeArray := []Time{
{
StandardTime: time.Now(),
},
{
StandardTime: time.Now().Add(30 * time.Minute),
},
}
c.JSON(http.StatusOK, timeArray)
})
_ = r.Run()
}
英文:
>
> func (p TypeName) MarshalJSON() ([]byte, error) {
> return []byte("Joe"), nil
> }
>
[]byte("Joe")
is not a valid json. It should be quoted. If you pay attention to the log of gin, you should see an error message there: Error #01: json: error calling MarshalJSON for type main.Employee: invalid character 'J' looking for beginning of value
. This one should work:
func (p TypeName) MarshalJSON() ([]byte, error) {
return []byte(`"Joe"`), nil
}
And here is a demo for time.Time
:
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type Time struct {
StandardTime time.Time
}
func (p Time) MarshalJSON() ([]byte, error) {
timeString := p.StandardTime.Format(time.RFC3339)
return json.Marshal(timeString)
}
func main() {
r := gin.Default()
r.GET("/time", func(c *gin.Context) {
timeArray := []Time{
{
StandardTime: time.Now(),
},
{
StandardTime: time.Now().Add(30 * time.Minute),
},
}
c.JSON(http.StatusOK, timeArray)
})
_ = r.Run()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论