packagemain import( "encoding/json" "fmt" _"time" ) typeHeaderstruct{ FileNamestring Hoststring Offsetstring Timestampstring } typeJsonDatastruct{ headerstring`json:"xx"` Bodystring`json:"body"` } funcmain(){ testJson() } functestJson(){ varDataJsonData /*time1:=time.Now().Unix() x:=Header{FileName:"yangheng",Host:"localhost",Offset:"123",Timestamp:string(time1)}*/ Data.headers="123" Data.Body="234" fmt.Printf("data%v\n",Data) lang,err:=json.Marshal(Data) iferr==nil{ fmt.Printf("result%v\n",string(lang)) } }
输出结果
data {123 234}
result {"body":"234"}
总之,要解析成json格式,struct里面变量必须大写