mirror of
https://github.com/json-iterator/go.git
synced 2025-04-23 11:37:32 +02:00
example unmarshal
This commit is contained in:
parent
26708bccc9
commit
3cf822853f
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
"os"
|
"os"
|
||||||
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleMarshal() {
|
func ExampleMarshal() {
|
||||||
@ -25,3 +26,22 @@ func ExampleMarshal() {
|
|||||||
// Output:
|
// Output:
|
||||||
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
// {"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 := json.Unmarshal(jsonBlob, &animals)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error:", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("%+v", animals)
|
||||||
|
// Output:
|
||||||
|
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user