简体   繁体   English

带有结构数组的 Golang 和 JSON

[英]Golang and JSON with array of struct

I would like to create a JSON of a GatewayInfo where 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.Marshal function:要创建 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 GatewayInfo instance has the SpanInfo , but in the JSON I have empty SpanInfo .正如我们所看到的, GatewayInfo实例具有SpanInfo ,但在 JSON 中我有空的SpanInfo

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.结构值编码为 JSON 对象。 Each exported struct field becomes a member of the object每个导出的 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

The json package can only serialize exported fields of your struct. json包只能序列化结构的导出字段。 Change your struct to start all fields with an uppercase letter so they can be included in the output:更改您的结构,以大写字母开头所有字段,以便它们可以包含在输出中:

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()的文档。


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"`
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM