Created
August 24, 2018 22:21
-
-
Save maniankara/6ab874cbb377b550ea3d83d46e5557bb to your computer and use it in GitHub Desktop.
Unmarshalling a arbitrary json list. There are several examples of nested list but not at root
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
// try here: https://play.golang.org/p/SChzGHXYgOU | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
func main() { | |
birdJson := `{"birds":[{"pigeon":"likes to perch on rocks","eagle":"bird of prey"}]}` | |
var result map[string]interface{} | |
json.Unmarshal([]byte(birdJson), &result) | |
birds := result["birds"].([]interface{}) | |
for _, key := range birds { | |
t := key.(map[string]interface{}) | |
for k, v := range t { | |
fmt.Println(k,v) | |
//pigeon likes to perch on rocks | |
//eagle bird of prey | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment