GO json 如何转化为 map 和 struct
type Event struct {
Type string `json:"type"`
Database string `json:"database"`
Table string `json:"table"`
Data []map[string]string `json:"data"`
}
func main () {
msg := []byte(`{
"type": "UPDATE",
"database": "blog",
"table": "blog",
"data": [
{
"blogId": "100001",
"title": "title",
"content": "this is a blog",
"uid": "1000012",
"state": "1"
}
]}`)
var event map[string]interface{}
if err := json.Unmarshal(msg, &event); err != nil {
panic(err)
}
fmt.Println(event)
}
e := Event{}
if err := json.Unmarshal(msg, &e); err != nil {
panic(err)
}
fmt.Println(e)