![cfba36d14d8ce1b98a0662b0fca0c70c.png](https://img-blog.csdnimg.cn/img_convert/cfba36d14d8ce1b98a0662b0fca0c70c.png)
原文发表于
json-iterator/go使用笔记yuchanns.org![4660b0d262db66dec607cd2824070521.png](https://img-blog.csdnimg.cn/img_convert/4660b0d262db66dec607cd2824070521.png)
json-iterator
是由滴滴开源的第三方json编码库,它同时提供Go和Java两个版本。
为什么使用
这个库具有很多优点。最常被人称道的就是性能高于充满反射的官方提供的编码库——据说在编码结构体时候,Go版本的效率是encoding/json
的6倍,而Java版本的效率是官方的3倍。
同时这个库还完全兼容官方库的api,替换官方库的方式不需要那么hack。
import jsoniter "github.com/json-iterator/go"
type Student struct {
ID uint `json:"id"`
Age uint8 `json:"age"`
Gender uint8 `json:"gender"`
Name string `json:"name"`
Location Location `json:"location"`
}
type Location struct {
Country string
Province string
City string
District string
}
var s = Student{
ID: 1,
Age: 27,
Gender: 1,
Name: "yuchanns",
Location: Location{
Country: "China",
Province: "Guangdong",
City: "Shenzhen",
District: "Nanshan",
},
}
func Marshal() {
// 使用ConfigCompatibleWithStandardLibrary完全兼容官方库
json := jsoniter.ConfigCompatibleWithStandardLibrary
result, _ := json.Marshal(&s)
println(result)
// output: {"id":1,"age":27,"gender":1,"name":"yuchanns","location":{"Country":"China","Province":"Guangdong","City":"Shenzhen","Distric