我正在尝试使用2个节点channel
和feeds
做出类似这样的响应,这是一个使用Go Lang的Gin HTTP Web框架的数组:
{
"channel": {
"id": "34HG34",
"name": "username"
},
"feeds": [
{
"timestamp": 147911578000,
"field1": 29.89
},
{
"timestamp": 147911578030,
"field1": 32.01
}
]
}
我无法显示channel
节点,但feeds
数组列表有效。这是我的代码:
type Response struct {
Channel ChannelType `json:"channel"`
Feed []FeedType `json:"feeds"`
}
type FeedType struct {
Timestamp int `json:"timestamp"`
Field1 float64 `json:"field1"`
}
type ChannelType struct {
id string `json:"id"`
name string `json:"name"`
}
channelA := ChannelType{
id: "hello",
name: "Savanna",
}
var FeedListA []FeedType
FeedListA = make([]FeedType, 0)
FeedListB = append(FeedListB, feed) // feed is something
router.GET("/hello", func(c *gin.Context) {
c.JSON(200, gin.H{
"channel": channelA,
"feeds": FeedListA,
})
})
这是channel
为空的json输出:
{
"channel": {
},
"feeds": [
{
"timestamp": 147911578000,
"field1": 32
}
]
}
如何在json响应中显示channel
节点?