Search code examples
jsongounmarshalling

go json.Unmarshal do not working


I have json which is not decoded to struct.

I know that error somewhere in my code, but I'm stuck and do not know where the error is and what am I doing wrong

Help me please, here is my code:

type GOOGLE_JSON struct {
    code        string `json:"code"`
    clientId    string `json:"clientId"`
    redirectUri string `json:"redirectUri"`
}

body := []byte(`{"code":"111","clientId":"222","redirectUri":"333"}`)

//google_json := GOOGLE_JSON{}
var google_json GOOGLE_JSON

err := json.Unmarshal(body, &google_json)
if err != nil {
    fmt.Println(err)
}
fmt.Println(google_json)

example here


Solution

  • I've found error

    was

        type GOOGLE_JSON struct {
            code        string `json:"code"`
            clientId    string `json:"clientId"`
            redirectUri string `json:"redirectUri"`
        }
    

    must be capital letters

        type GOOGLE_JSON struct {
            Code        string `json:"code"`
            ClientId    string `json:"clientId"`
            RedirectUri string `json:"redirectUri"`
        }
    

    I was inattentive

    Code // <- exported
    ClientId // <- exported
    RedirectUri // <- exported
    
    code // <-- not exported
    clientId // <-- not exported
    redirectUri // <-- not exported