package main
import("fmt""encoding/json")
type Monster struct{
Name string `json:"NAME_BIG"` //反射机制
Age int
Brithday string
Sal float64
Skill string
}//
func strcutToJson(){
moster := Monster{
Name:"qingqing",
Age:200,
Brithday:"202-10-10",
Sal:20.2,
Skill:"he tui",}//xuliehua monster
date,_ :=json.Marshal(&moster)
fmt.Printf("%v\n",string(date))}
func mapToJson(){//创建一个map,使用make初始化a
var a map[string]interface{}
a =make(map[string]interface{})
a["name"]="minmin"
a["age"]=20
a["addr"]="hangsu"
date,_ :=json.Marshal(a)//date数据需要转化为string类型
fmt.Printf("%v\n",string(date))}
func sliceToJson(){
var slice []map[string]interface{}//创建一个map,使用make初始化a
var a map[string]interface{}
a =make(map[string]interface{})
a["name"]="minmin"
a["age"]=20
a["addr"]="hangsu"
slice =append(slice,a)
var b map[string]interface{}
b =make(map[string]interface{})
b["name"]="bbb"
b["age"]=22
b["addr"]="asd"
slice =append(slice,b)
date,_ :=json.Marshal(slice)//date数据需要转化为string类型
fmt.Printf("%v\n",string(date))}
func main(){strcutToJson()mapToJson()sliceToJson()}