Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json Marshal对两个对象(Go/Golang)的行为不同_Json_Go_Struct_Marshalling - Fatal编程技术网

Json Marshal对两个对象(Go/Golang)的行为不同

Json Marshal对两个对象(Go/Golang)的行为不同,json,go,struct,marshalling,Json,Go,Struct,Marshalling,所以我想将数据封送到JSON。基本结构如下所示: type DatabaseObject struct { Preferences []int `json:"preferences"` Texts map[string]string `json:"texts"` Options map[string]string `json:"options"` Gender string `json:"

所以我想将数据封送到JSON。基本结构如下所示:

type DatabaseObject struct {
    Preferences []int             `json:"preferences"`
    Texts       map[string]string `json:"texts"`
    Options     map[string]string `json:"options"`
    Gender      string            `json:"gender"`
    EMail       string            `json:"email"`
}
以下是(工作)游乐场版本:

然而,当我在程序中使用这段代码时,结果却大不相同。这是我的密码:

jsonObject, err := json.Marshal(DatabaseObject{})
if err != nil {
    log.Fatal(err)
}
fmt.Printf("%+v", jsonObject)
它打印:

[123 34 112 114 101 102 101 114 101 110 99 101 115 34 58 110 117 108 108 44 34 116 101 120 116 115 34 58 110 117 108 108 44 34 111 112 116 105 111 110 115 34 58 110 117 108 108 44 34 103 101 110 100 101 114 34 58 34 34 44 34 101 109 97 105 108 34 58 34 34 125]
有人知道为什么json.Marshal在这里不工作吗?它是一个空结构,应该是这样的

{"preferences":null,"texts":null,"options":null,"gender":"","email":""}

您正试图用
%+v
打印
json.marshall
输出的内容的表示形式

返回一个字节片,它正是您要查看的内容

jsonObject, err := json.Marshal(DatabaseObject{})
if err != nil {
    log.Fatal(err)
}
fmt.Printf("%+v", jsonObject)
将打印JSON字符串的字节。如果改用
fmt.Printf(“%s”,jsonObject)
您将得到您想要的


另一个选项是
fmt.Printf(“%+v”,string(jsonObject))
,这样您就可以看到我在说什么,我已经修改了您提供的游乐场

我有点困惑,所以你是说如果你在操场上运行相同的代码,在本地它不会显示相同的输出?我编辑了原始问题,使其更简单。