Skip to content

Instantly share code, notes, and snippets.

@visualskyrim
Last active September 23, 2020 13:39
Show Gist options
  • Save visualskyrim/af21564dddaab4c1e317 to your computer and use it in GitHub Desktop.
Save visualskyrim/af21564dddaab4c1e317 to your computer and use it in GitHub Desktop.
golang - Unmarshal nested Json
package main
import (
"encoding/json"
)
type SomeJsonObj struct {
ID string `json:"id"`
Outter struct {
Mid struct {
Inner int `json:"_inner_"`
} `json:"mid"`
} `json:"outter"`
}
const (
jsonText = `
{
"id": "f289efd9324b4c30bd04a3d6b2877760",
"outter": {
"mid": {
"_inner_": 1000
}
}
}
`
)
func main() {
jsonObj := &SomeJsonObj{}
parseErr := json.Unmarshal(jsonText, jsonObj)
if parseErr != nil {
// deal with the error
}
}
@ashishtiwari1993
Copy link

corrected as parseErr := json.Unmarshal([]byte(jsonText), jsonObj)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment