Hi, I tried to write a REST API that returns a list of objects, If the objects are strings (or any simple built-in objects), it works fine. However, if the objects are user-defined-type, Context.JSON returns a list of EMPTY objects. It does not work when I either passing list as input to the Context.JSON or parsing the list and inserting objects into gin.H{}
Comment From: manucorporat
can you provide the source code?
Comment From: nguyener
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
type User struct{
last string
first string
}
func getUsers(c *gin.Context) {
var users = []User{User{"Smith", "John"}, User{"Doe", "Jane"}}
fmt.Println(users)
c.JSON(http.StatusOK, users)
}
}
func main(){
r := gin.Default()
r.GET("/users", getUsers)
r.Run(":8080")
}
Note: The line fmt.Println(users) prints out the data correctly. But the /users route does not
UPDATED by @manucorporat: added code highlighting
Comment From: manucorporat
Well, that is not a Gin issue. Unexpected fields (lower case) are not included in the JSON. http://stackoverflow.com/questions/28035784/golang-marshal-unmarshal-json-with-both-exported-and-un-exported-fields
http://blog.golang.org/json-and-go
Use this instead:
type User struct{
Last string `json:"last"`
First string `json:"first"`
}
Comment From: nguyener
Thanks
On Thu, May 7, 2015 at 10:42 AM, Manu Mtz.-Almeida <notifications@github.com
wrote:
Closed #282 https://github.com/gin-gonic/gin/issues/282.
— Reply to this email directly or view it on GitHub https://github.com/gin-gonic/gin/issues/282#event-299516972.
Comment From: grifondopala
F*** my life I tryed to fix it 2 days and the error was in first letter lower case