Skip to content

Instantly share code, notes, and snippets.

@geraldstanje
Forked from cuixin/json_to_map.go
Created September 20, 2019 15:53
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 geraldstanje/99423c2eb839d8b61cbdc08e51ca2de4 to your computer and use it in GitHub Desktop.
Save geraldstanje/99423c2eb839d8b61cbdc08e51ca2de4 to your computer and use it in GitHub Desktop.
json to map[string]interface{} example in golang
package main
import (
"encoding/json"
"fmt"
)
func dumpMap(space string, m map[string]interface{}) {
for k, v := range m {
if mv, ok := v.(map[string]interface{}); ok {
fmt.Printf("{ \"%v\": \n", k)
dumpMap(space+"\t", mv)
fmt.Printf("}\n")
} else {
fmt.Printf("%v %v : %v\n", space, k, v)
}
}
}
var jsonStr = `
{
"array": [
1,
2,
3
],
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "Hello World"
}
`
func main() {
jsonMap := make(map[string]interface{})
err := json.Unmarshal([]byte(jsonStr), &jsonMap)
if err != nil {
panic(err)
}
dumpMap("", jsonMap)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment