vorrei creare un JSON di un GatewayInfo
cui il tipo è definita in questo modo:Golang e JSON con array di struct
type SpanInfo struct {
imsi string
network string
network_status string
signal_quality int
slot int
state string
}
type GatewayInfo []SpanInfo
le informazioni sul gateway è creato con:
var gatewayInfo = make(GatewayInfo, nb_spans)
Per creare la JSON, io uso la funzione json.Marshal
:
gatewayInfo := getGatewayInfo(spans)
log.Printf("Polling content: %s\n", gatewayInfo)
jsonInfo, _ := json.Marshal(gatewayInfo)
log.Printf("jsonInfo: %s\n", jsonInfo)
Unfortuna tamente il risultato non è quello che mi aspettavo:
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: [{},{},{},{}]
Come possiamo vedere, l'istanza GatewayInfo
ha il SpanInfo
, ma nel JSON ho vuoto SpanInfo
.
possibile duplicato di [I miei strutture non sono di smistamento in JSON] (http://stackoverflow.com/questions/15452004/my-structures-are-not-marshalling-into-json) –