Reputation: 225
I have a really simple JSON file, something like this, but with thousands of strings:
{"fruits":["apple","banana","cherry","date"]}
and I want to load the fruits into a
map[string]interface{}
What is the best method? Is there a way where I don't need to iterate over each element and insert into the map using a loop?
Upvotes: 6
Views: 13791
Reputation: 249
here is example how you can Unmarshal to string list without any struct.
package main
import "fmt"
import "encoding/json"
func main() {
src_json := []byte(`{"fruits":["apple","banana","cherry","date"]}`)
var m map[string][]string
err := json.Unmarshal(src_json, &m)
if err != nil {
panic(err)
}
fmt.Printf("%v", m["fruits"][0]) //apple
}
Or instead of String list you can use
map[string][]interface{}
Upvotes: 10