Skip to content

Instantly share code, notes, and snippets.

@Sirpyerre
Created November 18, 2022 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sirpyerre/b4c7c5fc58c1116717bcf88b08d80848 to your computer and use it in GitHub Desktop.
Save Sirpyerre/b4c7c5fc58c1116717bcf88b08d80848 to your computer and use it in GitHub Desktop.
Unmarshal Json en GO
package main
import (
"encoding/json"
"fmt"
)
type AdminMenu struct {
Menu struct {
ID string `json:"id"`
Value string `json:"value"`
Popup Popup `json:"popup"`
} `json:"menu"`
}
type Popup struct {
Menuitem []struct {
Value string `json:"value"`
Onclick string `json:"onclick"`
} `json:"menuitem"`
}
func main() {
strJson := `{"menu":{"id":"file","value":"File","popup":{"menuitem":[{"value":"New","onclick":"CreateNewDoc()"},{"value":"Open","onclick":"OpenDoc()"},{"value":"Close","onclick":"CloseDoc()"}]}}}`
var adminMenu AdminMenu
err := json.Unmarshal([]byte(strJson), &adminMenu)
if err != nil {
fmt.Errorf("\nError unmarshal:%v", err)
}
fmt.Println("menu:", adminMenu)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment