简体   繁体   中英

How to set an array in json response goLang-gin

I have an array of stucts stored a variable my array.

Struct is

  type myStruct struct {
    id          int64   `db:"id" json:"id"`
    Name        string  `form:"name" db:"name" json:"name" binding:"required"`
    Status     string     `form:"status"  db:"status" json:"status" binding:"required"`

My array looks like this and is stored in a variable 'myArray'. This array is formed by iterating over a set of rows coming from database.

[{1 abc default} {2 xyz default}]

I am using gin as http server. How do I set this array into JSON reponse using c.JSON. Something like

[
   {
      id: 1,
      name : 'abc' 
      status: 'default'
   },
   {
      id: 2,
      name : 'xyz' 
      status: 'default'
   }
]

ok c.JSON(http.StatusOK, myArray) worked. But I cannot see the Id field in the response. Any reason why? Is it because of 'int64' dataType?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM