Iterate over json array in Go to extract values -
i have following in json array (conf.json file).
{ "repos": [ "a", "b", "c" ] }
i attempting read json , iterate on stuck. new go (and programming) having hard time understanding happening here.
import ( "encoding/json" "fmt" "os" ) type configuration struct { repos []string } func read_config() { file, _ := os.open("conf.json") decoder := json.newdecoder(file) configuration := configuration{} err := decoder.decode(&configuration) if err != nil { fmt.println("error:", err) } fmt.println(configuration.repos) }
so far far have been able get. print out values okay, [a, b, c]
.
what able iterate on array , split out each value individually have not had luck @ doing this. taking wrong approach this? there better way this?
you mean this:
for _, repo := range configuration.repos { fmt.println(repo) }
note code in example should not work json have given. there no mapping between value
, repos
. either have posted incorrect json or omitted tag on configuration
struct map correctly.
Comments
Post a Comment