标签:import enc https out def creat else ann github
jsoniter ( json-iterator )是一款快且灵活的 JSON 解析器
Jsoniter 是最快的 JSON 解析器。它最多能比普通的解析器快 10 倍之多,
独特的 iterator api 能够直接遍历 JSON ,极致性能! 0 内存分配!这样的 iterator 你绝对没有用过
github地址:
https://github.com/json-iterator/go
获取:
go get github.com/json-iterator/go
简单应用Marshal
由
json.Marshal(&data)
到
var json = jsoniter.ConfigCompatibleWithStandardLibrary json.Marshal(&data)
完整代码:
package main import ( "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,”Name”:”Reds”,”Colors”:[“Crimson”,”Red”,”Ruby”,”Maroon”]}
简单应用Unmarshal
由
json.Unmarshal(input, &data)
到
var json = jsoniter.ConfigCompatibleWithStandardLibrary json.Unmarshal(input, &data)
完整代码:
package main import ( "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}]
package main import ( "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) }
输出:
Crimson
package main import ( "fmt" "github.com/json-iterator/go" ) func main() { val := []byte(`{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`) str := jsoniter.Get(val, "Colors", 1).ToString() fmt.Println(str) }
输出:
Red
简单应用NewDecoder
package main import ( "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(¶ms) if err != nil { fmt.Println(err) } else { fmt.Printf("%+v\n", params) } }
输出:
map[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] size:3.8966875e+07 status:3 change_log:add the rows{10} create_time:2017-06-13 16:39:08 firmware_list: region:all release_flag:0 revision:62 md5:80dee2bf7305bcf179582088e29fd7b9 note:map[CoreServices:map[md5:d26975c0a8c7369f70ed699f2855cc2e package_name:CoreServices version_code:76 version_name:1.0.76] FrDaemon:map[md5:6b1f0626673200bc2157422cd2103f5d package_name:FrDaemon version_code:390 version_name:1.0.390] FrGallery:map[md5:90d767f0f31bcd3c1d27281ec979ba65 package_name:FrGallery version_code:349 version_name:1.0.349] FrLocal:map[package_name:FrLocal version_code:791 version_name:1.0.791 md5:f15a215b2c070a80a01f07bde4f219eb]] pack_version:1.5.3.344.393 branch:beta pack_version_code:393]
标签:import enc https out def creat else ann github
原文地址:https://www.cnblogs.com/saryli/p/11407289.html