Selaa lähdekoodia

example unmarshal

nifei 8 vuotta sitten
vanhempi
commit
3cf822853f
1 muutettua tiedostoa jossa 20 lisäystä ja 0 poistoa
  1. 20 0
      example_test.go

+ 20 - 0
example_test.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"github.com/json-iterator/go"
 	"os"
+	"encoding/json"
 )
 
 func ExampleMarshal() {
@@ -25,3 +26,22 @@ func ExampleMarshal() {
 	// 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 := json.Unmarshal(jsonBlob, &animals)
+	if err != nil {
+	fmt.Println("error:", err)
+	}
+	fmt.Printf("%+v", animals)
+	// Output:
+	// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
+}