go 字符串替换_json-iterator/go使用笔记

cfba36d14d8ce1b98a0662b0fca0c70c.png

原文发表于

json-iterator/go使用笔记​yuchanns.org
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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值