json4.go 751 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "encoding/json"
  7. "log"
  8. "reflect"
  9. )
  10. type FamilyMember struct {
  11. Name string
  12. Age int
  13. Parents []string
  14. }
  15. // STOP OMIT
  16. func Decode() {
  17. b := []byte(`{"Name":"Bob","Age":20,"Parents":["Morticia", "Gomez"]}`)
  18. var m FamilyMember
  19. err := json.Unmarshal(b, &m)
  20. // STOP OMIT
  21. if err != nil {
  22. panic(err)
  23. }
  24. expected := FamilyMember{
  25. Name: "Bob",
  26. Age: 20,
  27. Parents: []string{"Morticia", "Gomez"},
  28. }
  29. if !reflect.DeepEqual(expected, m) {
  30. log.Panicf("Error unmarshaling %q, expected %q, got %q", b, expected, m)
  31. }
  32. }
  33. func main() {
  34. Decode()
  35. }