json.Marshal返回奇怪的结果

huangapple go评论88阅读模式
英文:

json.Marshal returning strange results

问题

我正在尝试将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)
}

输出结果:

{Magical Mystery Zoo [{Cow Moo} {Cat Meow} {Fox ???}]}
[123 125]

期望的输出结果应该是:

{Magical Mystery Zoo [{Cow Moo} {Cat Meow} {Fox ???}]}
{
    "name" : "Magical Mystery Zoo",
    "animals" : [{
             "name" : "Cow",
             "says" : "moo"
         }, {
             "name" : "Cat",
             "says" : "Meow"
         }, {
             "name" : "Fox",
             "says" : "???"
         }
    ]
 }

[123 125] 是什么意思?

感谢您的帮助!

英文:

I'm trying to convert go structures to JSON. I thought I knew how to do it, but I'm confused by the output of this program. What am I missing?

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)
}

Output:

{Magical Mystery Zoo [{Cow Moo} {Cat Meow} {Fox ???}]}
[123 125]

Expected Output (something along the lines of):

{Magical Mystery Zoo [{Cow Moo} {Cat Meow} {Fox ???}]}
{
    "name" : "Magical Mystery Zoo",
    "animals" : [{
             "name" : "Cow",
             "says" : "moo"
         }, {
             "name" : "Cat",
             "says" : "Meow"
         }, {
             "name" : "Fox",
             "says" : "???"
         }
    ]
 }

Where is [123 125] coming from?

Appreciate the help!

答案1

得分: 13

编组的结果是[]byte,所以123125{}的ASCII码。

为了使编组工作,结构字段必须被导出:

每个导出的结构字段都成为对象的成员。

英文:

The result of marshaling is []byte so 123 and 125 is ascii for { and }

Struct fields have to be exported for marshaling to work:

> Each exported struct field becomes a member of the object

答案2

得分: 3

你的问题出在结构体中的未导出字段(如果你愿意,也可以称之为非公共字段)。这里是一个在Go Play上的示例,展示了如何解决这个问题。

此外,如果你不喜欢JSON字段的命名方式(大多数情况下是大写字母),你可以使用结构体标签来更改它们(参见json.Marshal文档)。

英文:

Your problems is in unexported (call it non-public if you want) fields in your structs. Here is the example on Go Play how you can fix it.

Also, if you don't like how your JSON fields named (capital letter in the most cases) you can change them with a struct tags (see json.Marshal doc).

huangapple
  • 本文由 发表于 2014年1月25日 06:12:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/21343463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定