Skip to content

Instantly share code, notes, and snippets.

@huytd
Created June 22, 2016 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huytd/15966f940d80c568eff4262697f4efb5 to your computer and use it in GitHub Desktop.
Save huytd/15966f940d80c568eff4262697f4efb5 to your computer and use it in GitHub Desktop.
Working with JSON in Golang
package main
import (
"fmt"
"encoding/json"
)
type (
Map map[string]interface{}
)
func main() {
obj := Map{
"name": "Huy Tran",
"year": 1991,
"made_in": Map{
"country": "Vietnam",
"city": "Danang",
},
"address": Map{
"country": "United States",
"state": "CA",
"city": "Milpitas",
},
"single": false,
}
jsonObj, _ := json.Marshal(obj)
fmt.Println("JSON Encode:\n", string(jsonObj))
fmt.Println("\n")
var parsed Map
jso := []byte(`{"address":"United States","made_in":{"country":"Vietnam", "city":"Danang"},"name":"Huy Tran","single":false,"year":1991}`)
if err := json.Unmarshal(jso, &parsed); err != nil {
fmt.Println("Error")
}
fmt.Println("JSON Decode:\n", parsed)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment