// code_032_json_unmarshal_to_interface project main.go package main import ( "encoding/json" "fmt" ) func main() { b := []byte(`{ "company":"tianmei", "subjects":[ "Go", "C++", "Python", "Test" ], "isok":true, "price":666.666 }`) //备注:subjects:["Test",]后面没有元素了,还有逗号会报错的。 var t interface{} err := json.Unmarshal(b, &t) if err != nil { fmt.Println("json err:", err) } fmt.Println(t) //使用断言判断类型 m := t.(map[string]interface{}) for k, v := range m { switch vv := v.(type) { case string: fmt.Println(k, "is string", vv) case int: fmt.Println(k, "is int", vv) case float64: fmt.Println(k, "is float64", vv) case bool: fmt.Println(k, "is bool", vv) case []interface{}: //slice类型的元素为interface{}接收 fmt.Println(k, "is an array:") for i, u := range vv { fmt.Println(i, u) } default: fmt.Println(k, "is of a type I don't know to handle") } } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Haskell School of Music
Paul Hudak、Donya Quick / Cambridge University Press / 2018-10-4 / GBP 42.99
This book teaches functional programming through creative applications in music and sound synthesis. Readers will learn the Haskell programming language and explore numerous ways to create music and d......一起来看看 《The Haskell School of Music》 这本书的介绍吧!
RGB转16进制工具
RGB HEX 互转工具
HTML 编码/解码
HTML 编码/解码