Last active
July 17, 2018 11:28
-
-
Save NaniteFactory/8ebddabedb3688840cb6b332ad3fdece to your computer and use it in GitHub Desktop.
Parse & convert JSON into a map
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" | |
| ) | |
| func main() { | |
| data := []byte(`{"sendMsg":{"user":"ANisus","msg":"Trying to send a message"},"say":"Hello"}`) | |
| fmt.Println("-----------------") | |
| var map_canonical interface{} | |
| err = json.Unmarshal(data, &map_canonical) | |
| fmt.Println(err, map_canonical) | |
| fmt.Println("-----------------") | |
| map_string_string := map[string]string{} | |
| json.Unmarshal(data, &map_string_string) | |
| for k, v := range map_string_string { | |
| fmt.Println(k + ":" + v) | |
| } | |
| fmt.Println("-----------------") | |
| map_string_rawjson := map[string]json.RawMessage{} | |
| json.Unmarshal(data, &map_string_rawjson) | |
| var submap map[string]string | |
| json.Unmarshal(map_string_rawjson["sendMsg"], &submap) | |
| for k, v := range submap { | |
| fmt.Println(k + ":" + v) | |
| } | |
| fmt.Println("-----------------") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment