Json unmarshal error

I get an error message:

json.Unmarshal undefined (type interface {} has no field or method Unmarshal)

trying to convert a piece of json byte into a generic interface type {}. I am reading docs for encoding/json and they give an example showing that this is valid. What gives?

 package main import ( "encoding/json" "fmt" "io/ioutil" ) func main() { var json interface{} data, _ := ioutil.ReadFile("testMusic.json") json.Unmarshal(data, &json) m := json.(map[string]interface{}) fmt.Printf("%+v", m) } 
+4
source share
1 answer

You have defined a local json variable that masks the global json character, referencing the JSON module. Renaming a local variable should allow your code to work.

+26
source

All Articles