我的模型具有以下数据:package maintype Subject struct { name string `json:name` section int `json:section`}var subjects = map[string][]Subject{ "1001": []Subject{ { name: "Phy", section: 1, }, { name: "Phy", section: 2, }, }, "1002": []Subject{ { name: "Chem", section: 1, }, { name: "Chem", section: 2, }, }, "1003": []Subject{ { name: "Math", section: 1, }, { name: "Math", section: 2, }, }, "1004": []Subject{ { name: "Bio", section: 1, }, { name: "Bio", section: 2, }, },}我正在创建路由,如下所示:route.GET("/subjects/:id", func(c *gin.Context) { id := c.Param("id") subjects := subjects[id] c.JSON(http.StatusOK, gin.H{ "StudentID": id, "Subject": subjects, }) })它试图用postman将其称为:localhost:8080/subjects/1001,但它只显示{} {}而不是主体结构的对象数组。输出: { “StudentID”: “1001”, “Subject”: [ {}, {} ] }
1 回答
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
这是因为您使用小写字段,因此不会被序列化。Subjectnamesection
将其更改为:
type Subject struct {
Name string `json:"name"`
Section int `json:"section"`
}
将显示以下字段:
{
"StudentID": "1001",
"Subject": [
{"name":"Phy","section":1},
{"name":"Phy","section":2}
]
}
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报
0/150
提交
取消