JSON
Converts any interface or string to JSON using Jsoniter.
JSON also sets the content header to application/json.
c.JSON(v interface{}) error
type SomeStruct struct {
Name string
Age uint8
}
app.Get("/json", func(c *fiber.Ctx) {
// Create data struct:
data := SomeStruct{
Name: "Grame",
Age: 20,
}
if err := c.JSON(data); err != nil {
c.Status(500).Send(err)
return
}
// => Content-Type: application/json
// => "{"Name": "Grame", "Age": 20}"
if err := c.JSON(fiber.Map{
"name": "Grame",
"age": 20,
}); err != nil {
c.Status(500).Send(err)
return
}
// => Content-Type: application/json
// => "{"name": "Grame", "age": 20}"
})