ja chce utworzyć JSON o GatewayInfo
gdzie typ są zdefiniowane następująco:Golang i JSON z tablicy struktury
type SpanInfo struct {
imsi string
network string
network_status string
signal_quality int
slot int
state string
}
type GatewayInfo []SpanInfo
Informacje brama jest utworzone z:
var gatewayInfo = make(GatewayInfo, nb_spans)
Aby stworzyć JSON, używam json.Marshal
funkcję:
gatewayInfo := getGatewayInfo(spans)
log.Printf("Polling content: %s\n", gatewayInfo)
jsonInfo, _ := json.Marshal(gatewayInfo)
log.Printf("jsonInfo: %s\n", jsonInfo)
Unfortuna dłuż- szej perspektywie wynik nie jest to, czego się spodziewałem:
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: [{},{},{},{}]
Jak widzimy, instancja GatewayInfo
ma SpanInfo
, ale w JSON mam pusty SpanInfo
.
możliwy duplikat [Kim struktury nie są etapowy w json] (http://stackoverflow.com/questions/15452004/my-structures-are-not-marshalling-into-json) –