Go实战--golang中使用号称全世界最快的JSON解析器json-iterator(json-iteratorgo)

Go实战--golang中使用号称全世界最快的JSON解析器json-iterator(json-iteratorgo),第1张

概述生命不止,继续go go go!!! 号外号外,差不一条广告,我的博客六月到十一月UV的地域分布: golang中的使用json一点也不陌生,golang为我们提供了官方的encoding/json: Go语言学习之encoding/json包(The way to go) Go实战–net/http中JSON的使用(The way to go) 今天,跟大家分享一个github上比较活的gola

生命不止,继续go go go!!!

号外号外,差不一条广告,我的博客六月到十一月UV的地域分布:

golang中的使用Json一点也不陌生,golang为我们提供了官方的enCoding/Json:

Go语言学习之encoding/json包(The way to go)

Go实战–net/http中JSON的使用(The way to go)

今天,跟大家分享一个github上比较活的golang Json解析器,也许是全世界最快的。

Jsoniter ( Json-iterator )是一款快且灵活的 JsON 解析器,同时提供 Java 和 Go 两个版本

Jsoniter 是最快的 JsON 解析器。它最多能比普通的解析器快 10 倍之多,即使在数据绑定的用法下也有同样的性能优势。无耻地献上自己的 跑分

非常易于使用的 API ,允许你使用任何风格或者混搭的方式来解析 JsON 。给你前所未有的灵活性。来看看这些 API 们 是不是真的有那么好用吧

独特的 iterator API 能够直接遍历 JsON ,极致性能! 0 内存分配!这样的 iterator 你绝对没有用过

golang中的跑分:

github地址:
https://github.com/json-iterator/go

获取:
go get github.com/Json-iterator/go

简单应用Marshal

Json.Marshal(&data)

var Json = Jsoniter.ConfigCompatibleWithStandardlibraryJson.Marshal(&data)

完整代码:

package mainimport (    "enCoding/Json"    "fmt"    "os"    "github.com/Json-iterator/go")func main() {    type colorGroup struct {        ID     int        name   string        colors []string    }    group := colorGroup{        ID:     1,name:   "Reds",colors: []string{"Crimson","Red","Ruby","Maroon"},}    b,err := Json.Marshal(group)    if err != nil {        fmt.Println("error:",err)    }    os.Stdout.Write(b)    var Json_iterator = Jsoniter.ConfigCompatibleWithStandardlibrary    b,err = Json_iterator.Marshal(group)    os.Stdout.Write(b)}

输出:
{“ID”:1,”name”:”Reds”,”colors”:[“Crimson”,”Red”,”Ruby”,”Maroon”]}{“ID”:1,”Maroon”]}

简单应用Unmarshal

Json.Unmarshal(input,&data)

var Json = Jsoniter.ConfigCompatibleWithStandardlibraryJson.Unmarshal(input,&data)

完整代码:

package mainimport (    "enCoding/Json"    "fmt"    "github.com/Json-iterator/go")func main() {    var JsonBlob = []byte(`[ {"name": "Platypus","Order": "Monotremata"},{"name": "Quoll","Order": "Dasyuromorphia"} ]`)    type Animal struct {        name  string        Order string    }    var animals []Animal    err := Json.Unmarshal(JsonBlob,&animals)    if err != nil {        fmt.Println("error:",err)    }    fmt.Printf("%+v",animals)    var animals2 []Animal    var Json_iterator = Jsoniter.ConfigCompatibleWithStandardlibrary    Json_iterator.Unmarshal(JsonBlob,&animals2)    fmt.Printf("%+v",animals2)}

输出:

[{name:Platypus Order:Monotremata} {name:Quoll Order:Dasyuromorphia}][{name:Platypus Order:Monotremata} {name:Quoll Order:Dasyuromorphia}]

简单应用Jsoniter.Get
一行代码完成解析:

package mainimport (    "fmt"    "github.com/Json-iterator/go")func main() {    val := []byte(`{"ID":1,"name":"Reds","colors":["Crimson","Red","Ruby","Maroon"]}`)    str := Jsoniter.Get(val,"colors", 0).ToString()    fmt.Println(str)}

简单应用NewDecoder

package mainimport (    "fmt"    "strings"    "github.com/Json-iterator/go")func main() {    Json := Jsoniter.ConfigCompatibleWithStandardlibrary    reader := strings.NewReader(`{"branch":"beta","change_log":"add the rows{10}","channel":"fros","create_time":"2017-06-13 16:39:08","firmware_List":"","md5":"80dee2bf7305bcf179582088e29fd7b9","note":{"CoreServices":{"md5":"d26975c0a8c7369f70ed699f2855cc2e","package_name":"CoreServices","version_code":"76","version_name":"1.0.76"},"FrDaemon":{"md5":"6b1f0626673200bc2157422cd2103f5d","package_name":"FrDaemon","version_code":"390","version_name":"1.0.390"},"Frgallery":{"md5":"90d767f0f31bcd3c1d27281ec979ba65","package_name":"Frgallery","version_code":"349","version_name":"1.0.349"},"FrLocal":{"md5":"f15a215b2c070a80a01f07bde4f219eb","package_name":"FrLocal","version_code":"791","version_name":"1.0.791"}},"pack_region_urls":{"CN":"https://s3.cn-north-1.amazonaws.com.cn/xxx-os/ttt_xxx_androID_1.5.3.344.393.zip","default":"http://192.168.8.78/ttt_xxx_androID_1.5.3.344.393.zip","local":"http://192.168.8.78/ttt_xxx_androID_1.5.3.344.393.zip"},"pack_version":"1.5.3.344.393","pack_version_code":393,"region":"all","release_flag":0,"revision":62,"size":38966875,"status":3}`)    decoder := Json.NewDecoder(reader)    params := make(map[string]interface{})    err := decoder.Decode(&params)    if err != nil {        fmt.Println(err)    } else {        fmt.Printf("%+v\n",params)    }}

输出:

map[firmware_List: note:map[CoreServices:map[package_name:CoreServices version_code:76 version_name:1.0.76 md5:d26975c0a8c7369f70ed699f2855cc2e] FrDaemon:map[md5:6b1f0626673200bc2157422cd2103f5d package_name:FrDaemon version_code:390 version_name:1.0.390] Frgallery:map[version_code:349 version_name:1.0.349 md5:90d767f0f31bcd3c1d27281ec979ba65 package_name:Frgallery] FrLocal:map[version_name:1.0.791 md5:f15a215b2c070a80a01f07bde4f219eb package_name:FrLocal version_code:791]] pack_version:1.5.3.344.393 pack_version_code:393 status:3 channel:fros pack_region_urls:map[CN:https://s3.cn-north-1.amazonaws.com.cn/xxx-os/ttt_xxx_androID_1.5.3.344.393.zip default:http://192.168.8.78/ttt_xxx_androID_1.5.3.344.393.zip local:http://192.168.8.78/ttt_xxx_androID_1.5.3.344.393.zip] release_flag:0 size:3.8966875e+07 md5:80dee2bf7305bcf179582088e29fd7b9 region:all revision:62 change_log:add the rows{10} create_time:2017-06-13 16:39:08 branch:beta]
总结

以上是内存溢出为你收集整理的Go实战--golang中使用号称全世界最快的JSON解析器json-iterator(json-iterator/go)全部内容,希望文章能够帮你解决Go实战--golang中使用号称全世界最快的JSON解析器json-iterator(json-iterator/go)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1273118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存