Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit 3cf8228

Browse files
author
nifei
committed
example unmarshal
1 parent 26708bc commit 3cf8228

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

example_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/json-iterator/go"
66
"os"
7+
"encoding/json"
78
)
89

910
func ExampleMarshal() {
@@ -25,3 +26,22 @@ func ExampleMarshal() {
2526
// Output:
2627
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
2728
}
29+
30+
func ExampleUnMarshal() {
31+
var jsonBlob = []byte(`[
32+
{"Name": "Platypus", "Order": "Monotremata"},
33+
{"Name": "Quoll", "Order": "Dasyuromorphia"}
34+
]`)
35+
type Animal struct {
36+
Name string
37+
Order string
38+
}
39+
var animals []Animal
40+
err := json.Unmarshal(jsonBlob, &animals)
41+
if err != nil {
42+
fmt.Println("error:", err)
43+
}
44+
fmt.Printf("%+v", animals)
45+
// Output:
46+
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
47+
}

0 commit comments

Comments
 (0)