Created
June 22, 2016 20:19
-
-
Save huytd/15966f940d80c568eff4262697f4efb5 to your computer and use it in GitHub Desktop.
Working with JSON in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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