Tags: #go #json #serialization
package main
import (
"encoding/json"
"fmt"
)
func main() {
var jsonBlob = []byte(`[
{"str": "Foo", "num": "1", "bool": "true", "its": 3},
{"str": "Bar", "num": "2", "bool": "false", "its": 4}
]`)
type Thing struct {
String string `json:"str"`
Number int `json:"num,string"`
Boolean bool `json:"bool,string"`
// IntToString string `json:"its,int"` // error: json: cannot unmarshal number into Go struct field Thing.its of type string
}
var things []Thing
err := json.Unmarshal(jsonBlob, &things)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%#v", things)
}