Created
November 18, 2022 00:17
-
-
Save Sirpyerre/b4c7c5fc58c1116717bcf88b08d80848 to your computer and use it in GitHub Desktop.
Unmarshal Json en GO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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