Go lang Gin中嵌套的json响应

时间:2016-12-05 08:08:59

标签: go

我正在尝试使用2个节点channelfeeds做出类似这样的响应,这是一个使用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节点?

0 个答案:

没有答案