Skip to content

Instantly share code, notes, and snippets.

@meddulla
Created November 10, 2013 17:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meddulla/7400923 to your computer and use it in GitHub Desktop.
Save meddulla/7400923 to your computer and use it in GitHub Desktop.
iterate over a go-simplejson map
import (
simplejson "github.com/bitly/go-simplejson"
"log"
)
func testMapIter () {
js, _ := simplejson.NewJson([]byte(`{
"test": {
"string_array": ["asdf", "ghjk", "zxcv"],
"omap": {"asdf": {"subkey": 1}, "ghjk": {"subkey": 2}, "zxcv":{"subkey": 3}},
"array": [1, "2", 3],
"arraywithsubs": [{"subkeyone": 1},
{"subkeytwo": 2, "subkeythree": 3}],
"int": 10,
"float": 5.150,
"string": "simplejson",
"bool": true
}
}`))
amap := js.Get("test").Get("omap")
for k, v := range amap.MustMap() {
ndata, _ := v.(map[string]interface{})
val, _ := ndata["subkey"]
log.Printf("%s key, %s", k, val)
}
}
func main () {
testMapIter()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment