package main import ( "encoding/json" "fmt" "strings" ) func main() { data := `{"name":"John","age":30,"city":"New York"}` var person map[string]interface{} decoder := json.NewDecoder(strings.NewReader(data)) decoder.Decode(&person) fmt.Println(person["name"]) // outputs: John fmt.Println(person["age"]) // outputs: 30 fmt.Println(person["city"]) // outputs: New York }
package main import ( "encoding/json" "fmt" "os" ) type Person struct { Name string `json:"name"` Age int `json:"age"` City string `json:"city"` } func main() { file, _ := os.Open("person.json") decoder := json.NewDecoder(file) var person Person decoder.Decode(&person) fmt.Println(person.Name) // outputs: John fmt.Println(person.Age) // outputs: 30 fmt.Println(person.City) // outputs: New York }In this example, we have defined a `Person` struct and used tags to indicate how the JSON data should be decoded into the struct. The `Decoder` type's `Decode()` method is used to decode the JSON data stored in a file into the `Person` struct. The input file is opened using the `os.Open()` function. These examples demonstrate the use of the `encoding/json` package's `Decoder` type to decode JSON data into Go values.