I want to unmarshal the following JSON into a struct:
{\"MAIN\":{\"data\":[{\"KEY1\":\"1111111\",\"KEY2\":\"2222222\",\"KEY3\":0,\"KEY4\":\"AAAAAAA\",\"KEY5\
This is happening because other packages (encoding/json
) can't access private fields (even with reflection). In go, private fields are fields starting with a lower case character. To fix this, make your struct contains public fields (which start with an upper case letter):
type jsonStruct struct {
Main struct {
Data []struct {
Key1 string `json:"KEY1"`
Key2 string `json:"KEY2"`
Key3 int `json:"KEY3"`
Key4 string `json:"KEY4"`
Key5 string `json:"KEY5"`
Key6 string `json:"KEY6"`
Key7 string `json:"KEY7"`
} `json:"data"`
} `json:"MAIN"`
}
https://play.golang.org/p/lStXAvDtpZ