我在Mac OS X 10.8.2上使用Go 1.0.3,并且我正在尝试使用该json软件包,试图将结构编组为json,但是我一直得到一个空的{}json对象。
该err值为nil,因此根据该json.Marshal函数,没有什么错,并且该结构正确。为什么会这样呢?
package main
import (
"encoding/json"
"fmt"
)
type Address struct {
street string
extended string
city string
state string
zip string
}
type Name struct {
first string
middle string
last string
}
type Person struct {
name Name
age int
address Address
phone string
}
func main() {
myname := Name{"Alfred", "H", "Eigenface"}
myaddr := Address{"42 Place Rd", "Unit 2i", "Placeton", "ST", "00921"}
me := Person{myname, 24, myaddr, "000 555-0001"}
b, err := json.Marshal(me)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(b)) // err is nil, but b is empty, why?
fmt.Println("\n")
fmt.Println(me) // me is as expected, full of data
}
相关分类