Golang中json和jsoniter的区别使用示例
目录
- 区别1: jsoniter除了支持字节数组与结构体互转外,还支持字符串与结构体互转
- 区别2: jsoniter能够处理类型不匹配情况
区别1: jsoniter除了支持字节数组与结构体互转外,还支持字符串与结构体互转
jsoniter使用示例:
package main import ( "fmt" "time" jsoniter "github.com/json-iterator/go" "github.com/json-iterator/go/extra" ) type Order struct { Id int64 `json:"id"` OrderId string `json:"order_id"` Money float64 `json:"money"` CreateTime time.Time `json:"create_time"` Extend map[string]string `json:"extend"` } func main() { order := Order{ Id: 666, OrderId: "12345678", Money: 99.99, CreateTime: time.Now(), Extend: map[string]string{"name": "张三"}, } // 使用1:直接转成字符串 jsonStr, _ := jsoniter.MarshalToString(order) fmt.Println("jsonStr:", jsonStr)python // 使用2:直接转成字节数组 jsonByteArr, _ := jsoniter.Marshal(order) fmt.Println("jsonByteArr:", jsonByteArr) // 使用3:反序列化之字符串转结构体 str := `{"id":666, "order_id":"12345678", "money": 99.99, "create_time":"2023-12-05T16:19:33.943989108+08:00", "extend":{"name":"张三"}}` var order2 Order err := jsoniter.UnmarshalFromString(str, &order2) if err != nil { fmt.Println("err2:", err) } fmt.Println("order2:", order2) // 使用4:反序列化之字节数组转结构体 var order3 Order var jsonNew = jsoniter.ConfigCompatibleWithStandardLibrary // 自适应类型 extra.RegisterFuzzyDecoders() err = jsonNew.Unmarshal([]byte(str), &order3) if err != nil { fmt.Println("err3:", err) } fmt.Println("order3:", order3) }
输编程出结果:
jsonStr: {"id":666,"order_id":"12345678","money":99.99,"create_time":"2023-12-05T16:20:40.595914198+08:00","extend":{"name":"张三"}}
jsonByteArr: [123 34 105 100 34 58 54 54 54 44 34 111 114 100 101 114 95 105 100 34 58 34 49 50 51 52 53 54 55 56 34 44 34 109 111 110 101 121 34 58 57 57 46 57 57 44 34 99 114 101 97 116 101 95 116 105 109 101 34 58 34 50 48 50 51 45 49 50 45 48 53 84 49 54 58 50 48 58 52 48 46 53 57 53 57 49 52 49 57 56 43 48 56 58 48 48 34 44 34 101 120 116 101 110 100 34 58 123 34 110 97 109 101 34 58 34 229 188 160 228 184 137 34 125 125]order2: {666 12345678 99.99 2023-12-05 16:19:33.943989108 +0800 CST map[name:张三]}order3: {666 12345678 99.99 2023-12-05 16:19:33.943989108 +0800 CST map[name:张三]}
区别2: jsonitjser能够处理类型不匹配情况
package main import ( "encoding/json" "fmt" "time" jsoniter "github.com/json-iterator/go" "github.com/json-iterator/go/extra" ) type Order struct { Id int64 `json:"id"` OrderId string `json:"order_id"` Money float64 `json:"money"` CreateTime time.Time `json:"create_time"` Extend map[string]string `json:"extend"` } func main() { str := `{"id":666, "order_id":"12345678", "money": "99.99", "create_time":"2023-12-05T16:19:33.943989108+08:00", "extend":{"name":"张三"}}` var order Order err := json.Unmarshal([]byte(str), &order) if err != nil { fmt.Println("json的err:", err) } fmt.Println("json的解析结果order:", order) var order2 Order var jsonNew = jsoniter.ConfigCompatibleWithStandardLibrary // 自适应类型 extra.RegisterFuzzyDecoders() err = jsonNew.Unmarshal([]byte(str), &order2) if err != nil { fmt.P编程rintln("jsoniter的err2:", err) } fmt.Println("jsoniter的解析结果order2:", order2) }
输出结果:
json的err: json: canno编程t unmarshal string into Go struct field Order.money of type float64
json的解析结果order: {666 12345678 0 2023-12-05 16:19:33.943989108 +0800 CST map[name:张三]}jsoniter的解析结果order2: {666 12345678 99.99 2023-12-05 16:19:33.943989108 +0800 CST map[name:张三]}
到此这篇关于golang中json和jsoniter的区别的文章就介绍到这了,更多相关Golang json和jsoniter内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论