2,348
社区成员




type ParentS struct {
father string
mother string
}
要改成:
type ParentS struct {
Father string
Mother string
}
最终代码如下:
package main
import (
"encoding/json"
"fmt"
)
type Message struct {
Name string
Body string
Time int64
Parents *ParentS
}
type ParentS struct {
Father string
Mother string
}
func main() {
pp := &ParentS{"Zhang", "li"}
m := Message{"Alice", "Hello", 1294706395881547000, pp}
fmt.Println("father is", m.Parents.Father)
x, err := json.Marshal(m)
if err != nil {
fmt.Printf("error is %v\n", err)
} else {
fmt.Printf("解析=%s", x)
}
}
输出信息:
father is Zhang
解析={"Name":"Alice","Body":"Hello","Time":1294706395881547000,"Parents":{"Father":"Zhang","Mother":"li"}}