我正在尝试将go结构转换为JSON.我以为我知道该怎么做,但我对这个程序的输出感到困惑.我错过了什么?
package main
import "fmt"
import "encoding/json"
type Zoo struct {
name string
animals []Animal
}
type Animal struct {
species string
says string
}
func main() {
zoo := Zoo{"Magical Mystery Zoo",
[]Animal {
{ "Cow", "Moo"},
{ "Cat", "Meow"},
{ "Fox", "???"},
},
}
zooJson, err := json.Marshal(zoo)
if (err != nil) {
fmt.Println(err)
}
fmt.Println(zoo)
fmt.Println(zooJson)
}
Run Code Online (Sandbox Code Playgroud)
输出:
{Magical Mystery Zoo [{Cow Moo} {Cat Meow} {Fox ???}]}
[123 125]
Run Code Online (Sandbox Code Playgroud)
预期产出(有些事情):
{Magical Mystery Zoo [{Cow Moo} {Cat Meow} {Fox ???}]}
{
"name" : "Magical Mystery Zoo",
"animals" : [{
"name" : "Cow",
"says" : "moo"
}, {
"name" : "Cat",
"says" : "Meow"
}, {
"name" : "Fox",
"says" : "???"
}
]
}
Run Code Online (Sandbox Code Playgroud)
[123 125]来自哪里?
感谢帮助!
| 归档时间: |
|
| 查看次数: |
844 次 |
| 最近记录: |