golang ---JSON-ITERATOR 使用
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]
golang ---JSON-ITERATOR 使用的更多相关文章
- Golang Json文件解析为结构体工具-json2go
代码地址如下:http://www.demodashi.com/demo/14946.html 概述 json2go是一个基于Golang开发的轻量json文件解析.转换命令行工具,目前支持转换输出到 ...
- golang json string remove field
golang中如何移除多余的field? 同样是json结构,不能像js 的json一样 delete key 直接移除,网上找了很多相似的,还没找到解决办法,先mark一下 感谢大神提供解决思路,设 ...
- golang json
1.Go语言的JSON 库 Go语言自带的JSON转换库为 encoding/json 1.1)其中把对象转换为JSON的方法(函数)为 json.Marshal(),其函数原型如下 func Mar ...
- golang json用法讲解
简介 json格式可以算我们日常最常用的序列化格式之一了,Go语言作为一个由Google开发,号称互联网的C语言的语言,自然也对JSON格式支持很好.但是Go语言是个强类型语言,对格式要求极其严格而J ...
- golang json html escape unicode
https://play.golang.org/p/FAH-XS-QMC https://github.com/gin-gonic/gin/issues/693 package main import ...
- golang json数组拼接
2016年06月16日 15:38:25 阅读数:2575 标签: golangjson数组 更多 个人分类: golang func main() { a := []byte(`{"P ...
- Golang JSON操作汇总
直接把结构体编码成json数据 package main import ( "encoding/json" "fmt" _ "os" ) t ...
- Golang ---json解析
golang官方为我们提供了标准的json解析库–encoding/json,大部分情况下,使用它已经够用了.不过这个解析包有个很大的问题–性能.它不够快,如果我们开发高性能.高并发的网络服务就无法满 ...
- golang json 包简单分析
首先上代码: func main() { b := true a1, _ := json.Marshal(b) a2, _ := Marshal(b) fmt.Println(string(a1)) ...
- kafka消息传输时的对象转字符串时所需 -json String 转list 、set、 Long、 String 、map 与json Iterator遍历
JSONObject jsonObject = new JSONObject(jsonString); Iterator iterator = jsonObject.keys(); while(ite ...
随机推荐
- http响应消息
1. 请求消息:客户端发送给服务器端的数据 * 数据格式: 1. 请求行 2. 请求头 3. 请求空行 4. 请求体 2. 响应消息:服务器端发送给客户端的数据 * 数据格式: 1. 响应行 1. 组 ...
- nginx return配置说明
该指令一般用于对请求的客户端直接返回响应状态码.在该作用域内return后面的所有nginx配置都是无效的. 可以使用在server.location以及if配置中. 除了支持跟状态码,还可以跟字符串 ...
- pycharm配置默认代码和注释模板
有人问,在pycharm新建python文件时,文件开头的注释每次都要重复写,能不能配置成模板?其实pycharm本身自带此功能 在pycharm菜单栏找File -> settings -&g ...
- 【技术博客】Git Flow模型管理代码版本
参考GIT版本管理:Git Flow模型,在此基础上加入了自己的理解,增加人员分工和相应代码,并根据本次项目的实际情况进行相应修改. 在本学期的软件工程开发过程中,我们从alpha阶段就使用了git ...
- 颜色空间模型(HSV\LAB\RGB\CMYK)
通过Photoshop的拾色器,我们知道表征颜色的模型的不止一种,本文将系统并且详细讨论这四种模型(HSV.LAB.RGB和CMYK)之间的联系以及应用.本文部分章节整合了多位优秀博主的博客(链接见本 ...
- svn 清除用户名和密码
- 第一节:Python+Selenium环境搭建
一.selenium工作原理 二.安装python Window系统下,python的安装很简单.访问python.org/download,下载最新版本,安装过程与其他windows软件类似.记得下 ...
- Consider defining a bean of type 'com.*.*.mapper.*.*Mapper' in your configuration.
@Mapper 不能加载的问题 Consider defining a bean of type 'com.*.*.mapper.*.*Mapper' in your configuration. 添 ...
- java继承实现的基本原理
方法调用的过程 寻找要执行的实例方法的时候,是从对象的实际类型信息开始查找的,找不到的时候,再查找父类类型信息. 动态绑定,而动态绑定实现的机制就是根据对象的实际类型查找要执行的方法,子类型中找不到的 ...
- Linux下安装.NET Core
环境 { "操作系统":"CentOS 7.5 64位", "CPU":"1核", "内存":&qu ...