Go 中的 JSON

Go 的标准库包含 encoding/json。

要点

Go 的标准库包含 encoding/json。

解析 JSON

import "encoding/json"
var user User
err := json.Unmarshal([]byte(text), &user)

序列化 JSON

text, err := json.MarshalIndent(user, "", "  ")

验证 JSON

if !json.Valid([]byte(text)) {
  log.Fatal("invalid json")
}