Unable to unmarshal JSON into struct

后端 未结 1 1253
囚心锁ツ
囚心锁ツ 2021-01-15 09:39

I want to unmarshal the following JSON into a struct:

{\"MAIN\":{\"data\":[{\"KEY1\":\"1111111\",\"KEY2\":\"2222222\",\"KEY3\":0,\"KEY4\":\"AAAAAAA\",\"KEY5\         


        
1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 10:30

    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

    0 讨论(0)
提交回复
热议问题