带有结构数组的 Golang 和 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28411394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:44:22  来源:igfitidea点击:

Golang and JSON with array of struct

jsongostruct

提问by Julio

I would like to create a JSON of a GatewayInfowhere the type are defined like this:

我想创建一个GatewayInfo类型定义如下的 JSON :

type SpanInfo struct {
    imsi string
    network string
    network_status string
    signal_quality int
    slot int
    state string
}

type GatewayInfo []SpanInfo

The gateway information is created with:

网关信息是通过以下方式创建的:

var gatewayInfo = make(GatewayInfo, nb_spans)

To create the JSON, I use the json.Marshalfunction:

要创建 JSON,我使用以下json.Marshal函数:

gatewayInfo := getGatewayInfo(spans)
log.Printf("Polling content: %s\n", gatewayInfo)

jsonInfo, _ := json.Marshal(gatewayInfo)
log.Printf("jsonInfo: %s\n", jsonInfo)

Unfortunately the result is not what I was expecting:

不幸的是,结果不是我所期望的:

2015/02/09 13:48:26 Polling content: [{652020105829193 20801 Registered (Roaming) %!s(int=17) %!s(int=2) } {652020105829194 20801 Registered (Roaming) %!s(int=16) %!s(int=3) } {652020105829192 20801 Registered (Roaming) %!s(int=19) %!s(int=1) } {652020105829197 20801 Registered (Roaming) %!s(int=19) %!s(int=4) }]
2015/02/09 13:48:26 jsonInfo: [{},{},{},{}]

As we can see, the GatewayInfoinstance has the SpanInfo, but in the JSON I have empty SpanInfo.

正如我们所看到的,GatewayInfo实例有SpanInfo,但在 JSON 中我有空SpanInfo

回答by Arjan

Your struct fields must be exported (field is exported if it begins with a capital letter) or they won't be encoded:

您的结构字段必须导出(如果以大写字母开头,则导出字段),否则它们不会被编码:

Struct values encode as JSON objects. Each exported struct field becomes a member of the object

结构值编码为 JSON 对象。每个导出的 struct 字段都成为对象的成员

To get the JSON representation as probably expected change the code to this:

要按预期获得 JSON 表示,请将代码更改为:

type SpanInfo struct {
    IMSI string `json:"imsi"`
    Network string `json:"network"`
    NetworkStatus string `json:"network_status"`
    SignalQuality int `json:"signal_quality"`
    Slot int `json:slot"`
    State string `json:"state"`
}

type GatewayInfo []SpanInfo

回答by icza

The jsonpackage can only serialize exported fields of your struct. Change your struct to start all fields with an uppercase letter so they can be included in the output:

json包只能序列化结构的导出字段。更改您的结构,以大写字母开头所有字段,以便它们可以包含在输出中:

type SpanInfo struct {
    Imsi string
    Network string
    Network_status string
    Signal_quality int
    Slot int
    State string
}

Read the documentation of json.Marshal()for details and more information.

json.Marshal()有关详细信息和更多信息,请阅读 的文档。

回答by Prafulla Girgaonkar



This is not a new answer. It is just consolidation of comments on the accepted answer.

这不是一个新的答案。这只是对已接受答案的评论的合并。



From ORIGINAL Query

来自原始查询

type SpanInfo struct {
    imsi string
    network string
    network_status string
    signal_quality int
    slot int
    state string
}

From Answer and comments - Please note that the first char of each field in struct is now in UPPER case along with json representation added to each field

来自答案和评论 - 请注意,结构中每个字段的第一个字符现在是大写,并且每个字段都添加了 json 表示

type SpanInfo struct {
    IMSI string `json:"imsi"`
    Network string `json:"network"`
    NetworkStatus string `json:"network_status"`
    SignalQuality int `json:"signal_quality"`
    Slot int `json:slot"`
    State string `json:"state"`
}