JSON Marshal and Unmarshal in Go
Encode and decode structs with the encoding/json stdlib.
Author:
chris
// Language: Go
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
raw := []byte(`{"name":"chris","age":31}`)
var u User
_ = json.Unmarshal(raw, &u)
u.Age++
out, _ := json.MarshalIndent(u, "", " ")
fmt.Println(string(out))
}