golang json判断类型
生活随笔
收集整理的這篇文章主要介紹了
golang json判断类型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
json怎么判斷類型
if q.Number == '0' {fmt.Println("q.Number is string!Pass" )}if q.Number == 0 {fmt.Println("q.Number is not string!Wrong" )}看上去很粗暴但是很實用,并沒有查到滿意的方法,待補充。
package main import ("encoding/json""fmt" )//type Product struct { // Name string // ProductID int64 // Number int // Price float64 // IsOnSale bool //} // Product _ type Product struct {Name string `json:"name"`ProductID int64 `json:"-"` // 表示不進行序列化Number int `json:"number, string"`Price float64 `json:"price, int"`IsOnSale bool `json:"_is_on_sale"` }var test struct {t interface{} }func main() {p := &Product{}p.Name = "Hodge"p.IsOnSale = falsep.Number = 0p.Price = 0.00p.ProductID = 3data, _ := json.Marshal(p)fmt.Println(string(data))// {"Name":"Hodge","ProductID":3,"Number":1,"Price":2,"IsOnSale":true}//t := test{}q := &Product{}json.Unmarshal([]byte(data), q)fmt.Println(*q)if q.Number == '0' {fmt.Println("q.Number is string!Pass" )}if q.Number == 0 {fmt.Println("q.Number is not string!Wrong" )} }總結
以上是生活随笔為你收集整理的golang json判断类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DNF魔法师如何加点?
- 下一篇: 11. 盛最多水的容器 golang