博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
『Go 内置库第一季:json』
阅读量:6647 次
发布时间:2019-06-25

本文共 4233 字,大约阅读时间需要 14 分钟。

大家好,我叫谢伟,是一名程序员。

近期我会持续更新内置库的学习笔记,主要参考的是文档 godoc 和 内置库的源码

在日常开发过程中,使用最频繁的当然是内置库,无数的开源项目,无不是在内置库的基础之上进行衍生、开发,所以其实是有很大的必要进行梳理学习。

本节的主题:内置库 json

大纲:

  • 自己总结的使用方法
  • 官方支持的API
  • 学到了什么

自己总结的用法

既然是 json 操作,那么核心应该是包括两个方面:

  • 序列化:go 数据类型转换为 json
  • 反序列化:json 转换为 go 数据类型

对应的方法:

  1. func Marshal(v interface{}) ([]byte, error)

  2. func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)

  3. func Unmarshal(data []byte, v interface{}) error

具体如何使用呢?

布尔类型

func boolToJson(ok bool) []byte {	jsonResult, _ := json.Marshal(ok)	return jsonResult}func jsonToBool(value []byte) bool {	var goResult bool	json.Unmarshal(value, &goResult)	return goResult}func main(){    fmt.Println(string(boolToJson(1 == 1)))	fmt.Println(jsonToBool([]byte(`true`)))}>>truetrue复制代码

数值型

func intToJson(value int) []byte {	jsonInt, _ := json.Marshal(value)	return jsonInt}func main(){    fmt.Println(string(intToJson(12)))}>> 12复制代码

结构体

结构体 转换为 json

type Info struct {	Name    string `json:"name,omitempty"`	Age     int    `json:"age,string"`	City    string `json:"city_shanghai"`	Company string `json:"-"`}func (i Info) MarshalOp() []byte {	jsonResult, _ := json.Marshal(i)	return jsonResult}func main(){	var info Info	info = Info{		Name:    "XieWei",		Age:     100,		City:    "shangHai",		Company: "Only Me",	}	fmt.Println(string(info.MarshalOp()))	var otherInfo Info	otherInfo.Name = ""	otherInfo.Age = 20	otherInfo.City = "BeiJing"	otherInfo.Company = "Only You"	fmt.Println(string(otherInfo.MarshalOp()))}>>{
"name":"XieWei","age":"100","city_shanghai":"shangHai"}{
"age":"20","city_shanghai":"BeiJing"}复制代码

还记得我们之间讲的 反射章节 结构体的 tag 吗?

  • info 结构体的 tag
  • omitempty 表示该字段为空时,不序列化
  • - 表示忽略该字段
  • json 内定义了该字段序列化时显示的字段,比如 Name 最后序列化 为 name;比如 City 最后序列化为 city_shanghai
  • json 内还可以转换类型,比如 age 原本 int 类型,最后转化为 string 类型

json 转换为 结构体:

func UnMarshalExample(value []byte) (result Info) {	json.Unmarshal(value, &result)	return result}func main(){   fmt.Println(UnMarshalExample([]byte(`{
"name":"xieWei", "age": "20", "city_shanghai": "GuangDong"}`)))}>>{xieWei 20 GuangDong }复制代码

好,至此,我们常用的 json 操作就这些,主要两个方面:Marshal 和 UnMarshal

大概讲述了下 结构体的 tag 的作用:

  • 比如如何定义字段名称
  • 比如如何忽略字段
  • 比如如何更改类型
  • 比如如何零值忽略

官方文档

列举几个再常用的:

  • func Valid(data []byte) bool
  • type Marshaler 接口,可以自己定义序列化的返回值
  • type Unmarshaler 接口,可以自己定义反序列化的返回值

Valid

判断是否是有效的 json 格式的数据

func main(){	fmt.Println(json.Valid([]byte(`{
"name":1, 2}`)))}>>false复制代码
  • 表示不是标准的 json 格式的数据

Marshaler 接口,需要实现 MarshalJSON 方法

自定义序列化返回值

type Marshaler interface {    MarshalJSON() ([]byte, error)}复制代码
type SelfMarshal struct {	Name string	Age  int	City string}func (self SelfMarshal) MarshalJSON() ([]byte, error) {	result := fmt.Sprintf("name:--%s,age:--%d,city:--%s", self.Name, self.Age, self.City)	if !json.Valid([]byte(result)) {		fmt.Println("invalid")		return json.Marshal(result)	}	return []byte(result), nil}func main(){        var self = SelfMarshal{}	self.Age = 20	self.Name = "XieWei"	self.City = "HangZhou"	selfJsonMarshal, err := json.Marshal(self)	fmt.Println(err, string(selfJsonMarshal))}>>
"name:--XieWei,age:--20,city:--HangZhou"复制代码
  • 返回了自定义的序列化的格式
type jsonTime time.Time//实现它的json序列化方法func (this jsonTime) MarshalJSON() ([]byte, error) {	var stamp = fmt.Sprintf("\"%s\"", time.Time(this).Format("2006-01-02 15:04:05"))	return []byte(stamp), nil}type Test struct {	Date  jsonTime `json:"date"`	Name  string   `json:"name"`	State bool     `json:"state"`}func main(){	var t = Test{}	t.Date = jsonTime(time.Now())	t.Name = "Hello World"	t.State = true	body, _ := json.Marshal(t)	fmt.Println(string(body))}>>{
"date":"2018-11-06 22:23:19","name":"Hello World","state":true}复制代码
  • 返回了自定义的序列化格式数据

总结

  • 友好的 API
  • 日常的序列化反序列化,内置的库其实已经满足要求,但是对于复杂的嵌套的数据类型,想要获取某个字段的值则相当费劲
  • 所以衍生了各种各样的号称高性能的 json 解析库

|

收获:

  • 可以自己定义序列化、反序列化的格式
  • 可以检测 是否符合 json 类型
func (self SelfMarshal) MarshalJSON() ([]byte, error) {	result := fmt.Sprintf("name:--%s,age:--%d,city:--%s", self.Name, self.Age, self.City)	return []byte(result), nil}复制代码

上文代码会报错,为什么?因为不是标准 json 格式的数据。

所以通常建议这么做:

func (self SelfMarshal) MarshalJSON() ([]byte, error) {	result := fmt.Sprintf("name:--%s,age:--%d,city:--%s", self.Name, self.Age, self.City)	if !json.Valid([]byte(result)) {		fmt.Println("invalid")		return json.Marshal(result)	}	return []byte(result), nil}复制代码

即将字符串 marshal 处理。


<完>

转载地址:http://hvrvo.baihongyu.com/

你可能感兴趣的文章
爱的世界里 一切都可以很真吗?
查看>>
JSP中获取当前访问URL、URL参数等信息的小结。
查看>>
前端相关整理
查看>>
PS下如何统计EXCHANGE的邮件负载
查看>>
BT下安装freeRDP
查看>>
linux系统服务器负载过高问题
查看>>
6月4日
查看>>
如何使用PhoneGap程序将AdMob广告嵌入到你的HTML5 iOS游戏中
查看>>
2014工作总结
查看>>
gluster分布式存储总结与实践
查看>>
我的友情链接
查看>>
Python爬虫爬数据写入到EXCEL中
查看>>
我的友情链接
查看>>
nginx的web目录下处理中文文件和文件夹以及编码格式问题
查看>>
Linux下Tomcat重新启动
查看>>
oracle rac 监听配置
查看>>
网络***简单介绍
查看>>
代理模式与动态代理
查看>>
MySQL 5.7 开启binary log(binlog)及注意事项
查看>>
Linux crontab 任务误删恢复及备份步骤
查看>>