package main import ( "encoding/json" "fmt" _ "time" ) type Header struct { FileName string Host string Offset string Timestamp string } type JsonData struct { header string `json:"xx"` Body string `json:"body"` } func main() { testJson() } func testJson() { var Data JsonData /*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) if err == nil { fmt.Printf("result %v \n", string(lang)) } }
输出结果
data {123 234}
result {"body":"234"}
总之,要解析成json格式,struct里面变量必须大写
有疑问加站长微信联系(非本文作者)