JSON02 GO language --- deserialization

package main

import (
	"encoding/json"
	"fmt"
)

type Human struct {
	Name string
	Age int
	Rmb float64
	Gender bool
	Hobbies []string
}

var jsonStr = `{
		"name":"梅川酷子",
		"age":40,
		"rmb":12345.123,
		"gender":false,
        "hobbies":["抽烟","喝酒","烫头"]
	}`

var jsonStr2 = `[
{
		"name":"梅川酷子",
		"age":40,
		"rmb":12345.123,
		"gender":false,
        "hobbies":["抽烟","喝酒","烫头"]
},
{
		"name":"朴成幸",
		"age":40,
		"rmb":12345.123,
		"Gender": to false, 
        "hobbies": [ "smoke", "drink", "hot head"] 
} 
] ` 

@ deserialize the json the Map 
FUNC main051 () { 
	retMap in: = the make (Map [String] {} interface) 
	ERR: = json.Unmarshal ([] byte (jsonStr), & retMap in) 
	! IF ERR = nil { 
		fmt.Println ( "deserialized failure, ERR =", ERR) 
		return 
	} 
	fmt.Println (retMap in) 
} 

// deserialize the json array slice 
FUNC main052 () { 
	retSlice: = the make ([] Map [String] interface {}, 0) 
	ERR: = json.Unmarshal ([] byte (jsonStr2), & retSlice) 
	IF ERR! = nil { 
		fmt.Println ( "deserialized failure, ERR =", ERR) 
		return 
	} 
	FMT.Println (retSlice) 
} 

/ * deserialize the objects json structure * / 
FUNC main053 () { 
	Human:= new(Human)
	ERR: = json.Unmarshal ([] byte (jsonStr), Human) 
	! IF ERR = nil { 
		fmt.Println ( "deserialized failure, ERR =", ERR) 
		return 
	} 
	fmt.Println (Human) 
} 

/ * The json array deserialized sections nested structure * / 
FUNC main () { 
	retSlice: = the make ([] Human, 0) 
	ERR: = json.Unmarshal ([] byte (jsonStr2), & retSlice) 
	IF ERR = nil! { 
		fmt.Println ( "deserialized failure, ERR =", ERR) 
		return 
	} 
	fmt.Println (retSlice) 
}

  

Guess you like

Origin www.cnblogs.com/yunweiqiang/p/11973804.html