我的模型具有以下數(shù)據(jù):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, }, },}我正在創(chuàng)建路由,如下所示: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,但它只顯示{} {}而不是主體結(jié)構(gòu)的對象數(shù)組。輸出: { “StudentID”: “1001”, “Subject”: [ {}, {} ] }
1 回答

qq_遁去的一_1
TA貢獻1725條經(jīng)驗 獲得超8個贊
這是因為您使用小寫字段,因此不會被序列化。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 關(guān)注
- 101 瀏覽
添加回答
舉報
0/150
提交
取消