Skip to content

Instantly share code, notes, and snippets.

@eliassoares
Forked from cuixin/json_to_map.go
Created September 17, 2018 00:10
Show Gist options
  • Save eliassoares/42ce54aac467d88e0eab351738e6ea74 to your computer and use it in GitHub Desktop.
Save eliassoares/42ce54aac467d88e0eab351738e6ea74 to your computer and use it in GitHub Desktop.

Revisions

  1. @cuixin cuixin created this gist Oct 25, 2017.
    46 changes: 46 additions & 0 deletions json_to_map.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    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)
    }