Il seguente codice unmarshal è "Id", ma non il "nome host". Perché? L'ho guardato abbastanza a lungo ora che se è un errore di battitura so che non lo vedrò mai. Aiuto per favore. (http://play.golang.org/p/DIRa2MvvAV)codifica/json unmarshal manca un campo
package main
import (
"encoding/json"
"fmt"
)
type jsonStatus struct {
Hostname string `json:host`
Id string `json:id`
}
func main() {
msg := []byte(`{"host":"Host","id":"Identifier"}`)
status := new(jsonStatus)
err := json.Unmarshal(msg, &status)
if err != nil {
fmt.Println("Unmarshall err", err)
}
fmt.Printf("Got status: %#v\n", status)
}
L'output che ottengo è
Got status: &main.jsonStatus{Hostname:"", Id:"Identifier"}
dove mi aspetto
Got status: &main.jsonStatus{Hostname:"Host", Id:"Identifier"}
Doh, non riesco a credere che ho perso. Inoltre non mi sono reso conto che i nomi delle chiavi non sono sensibili alle maiuscole e alle minuscole quando si parla di non maresciallo. Grazie! –