JSON

Converts any interface or string to JSON using Jsoniter.

JSON also sets the content header to application/json.
  1. c.JSON(v interface{}) error
  1. type SomeStruct struct {
  2. Name string
  3. Age uint8
  4. }
  5.  
  6. app.Get("/json", func(c *fiber.Ctx) {
  7. // Create data struct:
  8. data := SomeStruct{
  9. Name: "Grame",
  10. Age: 20,
  11. }
  12.  
  13. c.JSON(data)
  14. // => Content-Type: application/json
  15. // => "{"Name": "Grame", "Age": 20}"
  16.  
  17. c.JSON(fiber.Map{
  18. "name": "Grame",
  19. "age": 20,
  20. })
  21. // => Content-Type: application/json
  22. // => "{"name": "Grame", "age": 20}"
  23. })