package main import ( "encoding/json" "fmt" ) type Book struct { Title string Authors []string Publisher string IsPublisehed bool Price float64 } func main() { orgbook := Book{ "Go语言编程", {"xushiwei", "hughlv", "pandaman", "guangsong"}, "ituring.com.cn", true, 9.99} b, err := json.Marshal(orgbook) if err != nil { fmt.Println(err) } fmt.Println(string(b)) gobook := []byte(`{ "Title":"Go语言编程", "Authors":["xushiwei","hughlv", "pandaman","guangsong"], "Publisher":"ituring.com.cn", "IsPublished":true, "Price":9.99, "Sales":1000000 }`) var r interface{} err = json.Unmarshal(gobook, &r) if err != nil { fmt.Println(err) } //b, err := json.Marshal(gobook) //fmt.Println(gobook) //fmt.Println(b) gobook11, ok := r.(map[string]interface{}) if ok { for k, v := range gobook11 { switch v2 := v.(type) { case string: fmt.Println(k, "is a string:", v2) case int: fmt.Println(k, "is int:", v2) case bool: fmt.Println(k, "is bool:", v2) case []interface{}: fmt.Println(k, "is an array:") for i, iv := range v2 { fmt.Println(i, iv) } default: fmt.Println(k, "is another type:") } } } }