dongshetao1814
2015-02-09 13:56
浏览 807
已采纳

Golang和JSON与结构体数组

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
", gatewayInfo)

jsonInfo, _ := json.Marshal(gatewayInfo)
log.Printf("jsonInfo: %s
", 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.

图片转代码服务由CSDN问答提供 功能建议

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

  type SpanInfo struct {
 imsi字符串
网络字符串
 network_status字符串
 signal_quality int 
插槽int 
状态字符串
} 
 
type  GatewayInfo [] SpanInfo 
   
 
 

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

  var gatewayInfo = make(GatewayInfo,nb_spans  )
   
 
 

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

 < 代码> gatewayInfo:= getGatewayInfo(spans)
log.Printf(“投票内容:%s 
”,gatewayInfo)
 
jsonInfo,_:= json.Marshal(gatewayInfo)
log.Printf(“ jsonInfo:%s  
“,jsonInfo)
   
 
 

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

  2015/02  / 09 13:48:26轮询内容:[{652020105829193 20801已注册(漫游)%!s(int = 17)%!s(int = 2)} {652020105829194 20801已注册(漫游)%!s(int = 16)  %!s(int = 3)} {652020105829192 20801已注册(漫游)%!s(int = 19)%!s(int = 1)} {652020105829197 20801已注册(漫游)%!s(int = 19)%!  s(int = 4)}] 
2015 / 02/09 13:48:26 jsonInfo:[{},{},{},{}] 
   
 
 

如我们所见, GatewayInfo 实例具有 SpanInfo ,但是在JSON中,我有空的 SpanInfo < / DIV>

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题