英文:
How to create JSON for Go struct
问题
我正在尝试使用Marshal函数从Go结构体创建JSON。创建的JSON不包含Person结构体。我错过了什么?
type Person struct {
fn string
ln string
}
type ColorGroup struct {
ID int
Name string
Colors []string
P Person
}
per := Person{
fn: "John",
ln: "Doe",
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
P: per,
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
生成的输出如下:
{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"],"P":{}}
我在输出中没有看到Person。
英文:
I am trying to use the Marshal function to create JSON from a Go struct. The JSON created does not contain the Person struct.
What am I missing?
http://play.golang.org/p/ASVYwDM7Fz
type Person struct {
fn string
ln string
}
type ColorGroup struct {
ID int
Name string
Colors []string
P Person
}
per := Person{
fn: "John",
ln: "Doe",
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
P: per,
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
The output generated is as follows:
{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"],"P":{}}
I don't see Person in the output.
http://golang.org/pkg/encoding/json/#Marshal
答案1
得分: 16
你缺少两个要点。
- 只有公共字段可以被转换为 JSON。
- 写入 JSON 的名称是字段的名称。在这种情况下,字段 Person 的名称是
P
。
请注意,我将 Person
结构体的字段名称更改为大写,并在 ColorGroup
结构体上添加了一个 json
标签,以指示我希望该字段以另一个名称进行序列化。通常会对大多数字段进行标记,并将名称更改为小写以与 JavaScript 的风格保持一致。
以下是翻译好的代码:
package main
import (
"encoding/json"
"fmt"
"os"
)
func main() {
type Person struct {
Fn string
Ln string
}
type ColorGroup struct {
ID int
Name string
Colors []string
P Person `json:"Person"`
}
per := Person{
Fn: "John",
Ln: "Doe",
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
P: per,
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
}
输出结果为:
{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"],"Person":{"Fn":"John","Ln":"Doe"}}
英文:
You are missing two things.
- Only Public fields can be Marshaled to json.
- The name written to json is the name of the fieldd. In this case
P
for the field Person.
Notice that I changed the Fields name to be capital for the Person
struct and that I added
a tag
json on the ColorGroup Struct to indicate that I want that field to be serialized with another name. Is common to tag most of the fields and change the name to lowercase to be in sync with javascript's style.
http://play.golang.org/p/HQQ8r8iV7l
package main
import (
"encoding/json"
"fmt"
"os"
)
func main() {
type Person struct {
Fn string
Ln string
}
type ColorGroup struct {
ID int
Name string
Colors []string
P Person `json:"Person"`
}
per := Person{Fn: "John",
Ln: "Doe",
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
P: per,
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
}
Will output
{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"],"Person":{"Fn":"John","Ln":"Doe"}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论