Struct Conversion

The Struct method is used to convert the entire data content contained in Json into a specified data format or object.

  1. data :=
  2. `
  3. {
  4. "count" : 1,
  5. "array" : ["John", "Ming"]
  6. }`
  7. if j, err := gjson.DecodeToJson(data); err != nil {
  8. panic(err)
  9. } else {
  10. type Users struct {
  11. Count int
  12. Array []string
  13. }
  14. users := new(Users)
  15. if err := j.Scan(users); err != nil {
  16. panic(err)
  17. }
  18. fmt.Printf(`%+v`, users)
  19. }
  20. // Output:
  21. // &{Count:1 Array:[John Ming]}