package main import ( "encoding/json" "fmt" ) type IT struct { Company string `json:"company"` Subjects []string `json:"subjects"` Isok bool `json:"isok"` Price float64 `json:"price"` } func main() { jsonbuf := `{"company":"zyg","isok":true,"price":5.55,"subjects":["go","python","java"]}` var tmp IT err := json.Unmarshal([]byte(jsonbuf), &tmp) //這個地方一下是取tmp的地址的,它需要對結構體進行更改,根據查看源碼可以知道它的返回就一個err if err != nil { return } fmt.Printf("tmp = %+v\n", tmp) }
執行的結果為
tmp = {Company:zyg Subjects:[go python java] Isok:true Price:5.55} //這個結果沒有問題,打印就是沒有雙引號的
如果其中只想需打印結果體的下面兩行,只需要修改結構體為
type IT struct { //Company string `json:"company"` //Subjects []string `json:"subjects"` Isok bool `json:"isok"` Price float64 `json:"price"` }
那么執行的結果自動的解析 為
tmp = {Isok:true Price:5.55}