六、json支持

package mainimport (   "encoding/json"   "fmt")// Json 支持type Response1 struct{   Page int   Fruits []string}type Response2 struct{   Page int `json:"page"`   Fruits []string `json:"fruits"`}func main(){   bolB,_:=json.Marshal(true)   fmt.Println(string(bolB))   intB,_:=json.Marshal(1)   fmt.Println(string(intB))   fltB,_:=json.Marshal(2.34)   fmt.Println(string(fltB))   strB,_:=json.Marshal("gopher")   fmt.Println(string(strB))   slcD:=[]string{"apple","peach","pear"}   slcB,_:=json.Marshal(slcD)   fmt.Println(string(slcB))   //var map_vaiable  map[key_data_type]value_data_type   mapD :=map[string] int{"apple":5,"lettuce":3}   mapB,_:=json.Marshal(mapD)   fmt.Println(string(mapB))   res1D:=&Response1{      Page:1,      Fruits:[]string{"apple","peach","pear"},   }   res1B,_:=json.Marshal(res1D)   fmt.Println(string(res1B))   res2D :=Response2{      Page:1,      Fruits:[]string{"apple","peach","pear"},   }   res2B,_:=json.Marshal(res2D)   fmt.Println(string(res2B))   byt:=[]byte(`{"num":6.13,"strs":["a","b"]}`)   var dat map[string]interface{}   if err:=json.Unmarshal(byt,&dat);err!=nil{      panic(err)   }   fmt.Println(dat)   //轉成合適類型   num:=dat["num"].(float64)   fmt.Println(num)   strs :=dat["strs"].([]interface{})   str1:=strs[0].(string)   fmt.Println(str1)}
相關文章
相關標籤/搜索