Golang和JSON的结构数组

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

Golang and JSON with array of struct

问题

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

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

type GatewayInfo []SpanInfo

使用以下代码创建网关信息:

var gatewayInfo = make(GatewayInfo, nb_spans)

为了创建JSON,我使用了json.Marshal函数:

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

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

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

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: [{},{},{},{}]

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

英文:

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

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:

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.

答案1

得分: 46

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

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

为了获得预期的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
英文:

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

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

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

答案2

得分: 12

json 包只能序列化你的结构体中的公开字段。将你的结构体中的所有字段改为以大写字母开头,这样它们就可以包含在输出中:

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

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

英文:

The json package 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:

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.

答案3

得分: 0

这不是一个新的答案,只是对接受答案的评论进行整理。

从原始查询中:

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

从答案和评论中 - 请注意,结构体中每个字段的首字母现在都是大写的,并且每个字段都添加了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"`
}
英文:

> 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

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

huangapple
  • 本文由 发表于 2015年2月9日 21:56:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/28411394.html
匿名

发表评论

匿名网友

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

确定