因为我一直是一名前端工程师,因此我会在前端的角度将js中的json操做和go中的操做作一个类比方便你们的理解。前端
js : JSON.parse(
`{"name":"cfl"}`)
go : json.Unmarshal
golang
jsonStr := `{ "name":"cfl", "age":10, "friend":[{"name":"sx", "age":10 }] }` type User struct { Name string Age int64 Friend []User } var cfl User json.Unmarshal([]byte(jsonStr), &cfl) fmt.Printf("%+v", cfl)
js: JSON.stringify
go: json.Marshal
json
type User struct { Name string Age int64 Friend []User } cfl := User{ Name: "cfl", Age: 10, Friend: []User{ {Name: "sx", Age: 10}, }, } jsonStr, _ := json.Marshal(cfl) fmt.Printf("%v\n", string(jsonStr))
json.Valid(byte[]) //校验json字符串是否合法 json.Indent //按照必定的格式缩进 json.Compact //压缩 json.MarshalIndent //转换回带缩进的json字符串
参考资料 encoding/jso前端工程师