1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package jsoniter_test
- import (
- "fmt"
- "os"
- "github.com/json-iterator/go"
- )
- func ExampleMarshal() {
- type ColorGroup struct {
- ID int
- Name string
- Colors []string
- }
- group := ColorGroup{
- ID: 1,
- Name: "Reds",
- Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
- }
- b, err := jsoniter.Marshal(group)
- if err != nil {
- fmt.Println("error:", err)
- }
- os.Stdout.Write(b)
- // Output:
- // {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
- }
- func ExampleUnmarshal() {
- var jsonBlob = []byte(`[
- {"Name": "Platypus", "Order": "Monotremata"},
- {"Name": "Quoll", "Order": "Dasyuromorphia"}
- ]`)
- type Animal struct {
- Name string
- Order string
- }
- var animals []Animal
- err := jsoniter.Unmarshal(jsonBlob, &animals)
- if err != nil {
- fmt.Println("error:", err)
- }
- fmt.Printf("%+v", animals)
- // Output:
- // [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
- }
|