英文:
My structures are not marshalling into json
问题
我正在使用Go 1.0.3在Mac OS X 10.8.2上,并且正在尝试使用json
包将结构体编组为json,但是我一直得到一个空的{}
json对象。
根据json.Marshal
函数,err
值为nil,所以没有任何问题,结构体也是正确的。为什么会发生这种情况?
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
}
英文:
I am using Go 1.0.3 on Mac OS X 10.8.2, and I am experimenting with the json
package, trying to marshal a struct to json, but I keep getting an empty {}
json object.
The err
value is nil, so nothing is wrong according to the json.Marshal
function, and the struct is correct. Why is this happening?
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
}
答案1
得分: 44
你必须将你想要进行编组的字段设置为公共的。
像这样:
type Address struct {
Street string
Extended string
City string
State string
Zip string
}
err
是 nil
,因为所有的导出字段(在这种情况下没有)都被正确地编组了。
工作示例:https://play.golang.org/p/9NH9Bog8_C6
查看文档:http://godoc.org/encoding/json/#Marshal
英文:
You have to make the fields that you want to marshal public.
Like this:
type Address struct {
Street string
Extended string
City string
State string
Zip string
}
err
is nil
because all the exported fields, in this case there are none, were marshalled correctly.
Working example: https://play.golang.org/p/9NH9Bog8_C6
Check out the docs http://godoc.org/encoding/json/#Marshal
答案2
得分: 5
请注意,您还可以通过以下方式来操作生成的JSON中字段的名称:
type Name struct {
First string `json:"firstname"`
Middle string `json:"middlename"`
Last string `json:"lastname"`
}
英文:
Note that you can also manipulate what the name of the fields in the generated JSON are by doing the following:
type Name struct {
First string `json:"firstname"`
Middle string `json:"middlename"`
Last string `json:"lastname"`
}
答案3
得分: 1
JSON库无法查看结构体中的字段,除非它们是公开的。在你的情况下,字段name、age、address和phone不是公开的(以小写字母开头)。在Go语言中,变量/函数是公开的,当它们以大写字母开头时。所以为了使它工作,你的结构体需要像这样:
type Person struct {
Name Name
Age int
Address Address
Phone string
}
英文:
JSON library cannot view the fields in a struct unless they are public. In your case,
type Person struct {
name Name
age int
address Address
phone string
}
the fields name, age, address and phone are not public (start with a small letter). In golang, variables/functions are public, when they start with a capital letter. So for this to work, your struct needs to look something like this:
type Person struct {
Name Name
Age int
Address Address
Phone string
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论