简介

json格式可以算我们日常最常用的序列化格式之一了,Go语言作为一个由Google开发,号称互联网的C语言的语言,自然也对JSON格式支持很好。但是Go语言是个强类型语言,对格式要求极其严格而JSON格式虽然也有类型,但是并不稳定,Go语言在解析来源为非强类型语言时比如PHP等序列化的JSON时,经常遇到一些问题诸如字段类型变化导致无法正常解析的情况,导致服务不稳定。所以本篇的主要目的

  1. 就是挖掘Golang解析json的绝大部分能力
  2. 比较优雅的解决解析json时存在的各种问题
  3. 深入一下Golang解析json的过程
  • Golang解析JSON之Tag篇

  1. 一个结构体正常序列化过后是什么样的呢?

package main
import (
"encoding/json"
"fmt"
) // Product 商品信息
type Product struct {
Name string
ProductID int64
Number int
Price float64
IsOnSale bool
} func main() {
p := &Product{}
p.Name = "Xiao mi 6"
p.IsOnSale = true
p.Number = 10000
p.Price = 2499.00
p.ProductID = 1
data, _ := json.Marshal(p)
fmt.Println(string(data))
} //结果
{"Name":"Xiao mi 6","ProductID":1,"Number":10000,"Price":2499,"IsOnSale":true}

  2. 何为Tag,tag就是标签,给结构体的每个字段打上一个标签,标签冒号前是类型,后面是标签名

// Product _
type Product struct {
Name string `json:"name"`
ProductID int64 `json:"-"` // 表示不进行序列化
Number int `json:"number"`
Price float64 `json:"price"`
IsOnSale bool `json:"is_on_sale,string"`
} // 序列化过后,可以看见
{"name":"Xiao mi 6","number":10000,"price":2499,"is_on_sale":"false"}

  3. omitempty,tag里面加上omitempy,可以在序列化的时候忽略0值或者空值

package main

import (
"encoding/json"
"fmt"
) // Product _
type Product struct {
Name string `json:"name"`
ProductID int64 `json:"product_id,omitempty"`
Number int `json:"number"`
Price float64 `json:"price"`
IsOnSale bool `json:"is_on_sale,omitempty"`
} func main() {
p := &Product{}
p.Name = "Xiao mi 6"
p.IsOnSale = false
p.Number = 10000
p.Price = 2499.00
p.ProductID = 0 data, _ := json.Marshal(p)
fmt.Println(string(data))
}
// 结果
{"name":"Xiao mi 6","number":10000,"price":2499}

  4. type,有些时候,我们在序列化或者反序列化的时候,可能结构体类型和需要的类型不一致,这个时候可以指定,支持string,number和boolean

package main

import (
"encoding/json"
"fmt"
) // Product _
type Product struct {
Name string `json:"name"`
ProductID int64 `json:"product_id,string"`
Number int `json:"number,string"`
Price float64 `json:"price,string"`
IsOnSale bool `json:"is_on_sale,string"`
} func main() { var data = `{"name":"Xiao mi 6","product_id":"10","number":"10000","price":"2499","is_on_sale":"true"}`
p := &Product{}
err := json.Unmarshal([]byte(data), p)
fmt.Println(err)
fmt.Println(*p)
}
// 结果
<nil>
{Xiao mi 6 10 10000 2499 true}
  • 下面讲一讲Golang如何自定义解析JSON,Golang自带的JSON解析功能非常强悍

说明

很多时候,我们可能遇到这样的场景,就是远端返回的JSON数据不是你想要的类型,或者你想做额外的操作,比如在解析的过程中进行校验,或者类型转换,那么我们可以这样或者在解析过程中进行数据转换

实例

package main

import (
"bytes"
"encoding/json"
"fmt"
) // Mail _
type Mail struct {
Value string
} // UnmarshalJSON _
func (m *Mail) UnmarshalJSON(data []byte) error {
// 这里简单演示一下,简单判断即可
if bytes.Contains(data, []byte("@")) {
return fmt.Errorf("mail format error")
}
m.Value = string(data)
return nil
} // UnmarshalJSON _
func (m *Mail) MarshalJSON() (data []byte, err error) {
if m != nil {
data = []byte(m.Value)
}
return
} // Phone _
type Phone struct {
Value string
} // UnmarshalJSON _
func (p *Phone) UnmarshalJSON(data []byte) error {
// 这里简单演示一下,简单判断即可
if len(data) != 11 {
return fmt.Errorf("phone format error")
}
p.Value = string(data)
return nil
} // UnmarshalJSON _
func (p *Phone) MarshalJSON() (data []byte, err error) {
if p != nil {
data = []byte(p.Value)
}
return
} // UserRequest _
type UserRequest struct {
Name string
Mail Mail
Phone Phone
} func main() {
user := UserRequest{}
user.Name = "ysy"
user.Mail.Value = "yangshiyu@x.com"
user.Phone.Value = "18900001111"
fmt.Println(json.Marshal(user))
}

  

为什么要这样?

如果是客户端开发,需要开发大量的API,接收大量的JSON,在开发早期定义各种类型看起来是很大的工作量,不如写 if else 判断数据简单暴力。但是到开发末期,你会发现预先定义的方式能极大的提高你的代码质量,减少代码量。下面实例1和实例2,谁能减少代码一目了然

 实例1,if else做数据校验
// UserRequest _
type UserRequest struct {
Name string
Mail string
Phone string
}
func AddUser(data []byte) (err error) {
user := &UserRequest{}
err = json.Unmarshal(data, user)
if err != nil {
return
}
//
if isMail(user.Mail) {
return fmt.Errorf("mail format error")
} if isPhone(user.Phone) {
return fmt.Errorf("phone format error")
} // TODO
return
} 实例2,利用预先定义好的类型,在解析时就进行判断
// UserRequest _
type UserRequest struct {
Name string
Mail Mail
Phone Phone
} func AddUser(data []byte) {
user := &UserRequest{}
err = json.Unmarshal(data, user)
if err != nil {
return
} // TODO }

  转自:http://www.cnblogs.com/yangshiyu/p/6942414.html

https://www.cnblogs.com/52php/p/6518728.html

golang json用法讲解的更多相关文章

  1. .NET3.5中JSON用法以及封装JsonUtils工具类

    .NET3.5中JSON用法以及封装JsonUtils工具类  我们讲到JSON的简单使用,现在我们来研究如何进行封装微软提供的JSON基类,达到更加方便.简单.强大且重用性高的效果. 首先创建一个类 ...

  2. Mysql优化_慢查询开启说明及Mysql慢查询分析工具mysqldumpslow用法讲解

    Mysql优化_慢查询开启说明及Mysql慢查询分析工具mysqldumpslow用法讲解   Mysql慢查询开启 Mysql的查询讯日志是Mysql提供的一种日志记录,它用来记录在Mysql中响应 ...

  3. C++通过jsoncpp类库读写JSON文件-json用法详解

    介绍: JSON 是常用的数据的一种格式,各个语言或多或少都会用的JSON格式. JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择. ...

  4. Golang Json文件解析为结构体工具-json2go

    代码地址如下:http://www.demodashi.com/demo/14946.html 概述 json2go是一个基于Golang开发的轻量json文件解析.转换命令行工具,目前支持转换输出到 ...

  5. JSON: JSON 用法

    ylbtech-JSON: JSON 用法 1. JSON Object creation in JavaScript返回顶部 1. <!DOCTYPE html> <html> ...

  6. json用法常见错误

    Json用法三个常见错误   net.sf.json.JSONException: java.lang.NoSuchMethodException

  7. 问题:c# newtonsoft.json使用;结果:Newtonsoft.Json 用法

    Newtonsoft.Json 用法 Newtonsoft.Json 是.NET 下开源的json格式序列号和反序列化的类库.官方网站: http://json.codeplex.com/ 使用方法 ...

  8. Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析

    Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...

  9. (27)Cocos2d-x 3.0 Json用法

    Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...

随机推荐

  1. Apache 强制Http跳转Https

    找到网站根目录的.htaccess文件,添加如下代码 RewriteEngine On RewriteCond %{SERVER_PORT} RewriteRule ^(.*)$ https://%{ ...

  2. [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  3. node使用 mongoose聚合 group

    var mongoose = require('mongoose'); mongoose.connect("mongodb://localhost:27017/test", fun ...

  4. SmartGit 过期破解 - 授权文件 Free Trial License to Non-Commercial

    亲测可用~ Windows: %APPDATA%/syntevo/SmartGit/OS X:    ~/Library/Preferences/SmartGit/Unix/Linux:  ~/.sm ...

  5. 编译snort经验

    google搜索,找个感觉挺新的版本 https://zh.osdn.net/frs/g_redir.php?m=netix&f=%2Fslackbuildsdirectlinks%2Fsno ...

  6. 12月centos单词

    ---恢复内容开始--- UNreachable:adj.(network server unreachable) 不能达到的; 及不到的; 取不到的; 不能得到的; rsync: rsync [re ...

  7. node.js初识10

    post请求 form.html <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  8. python 定义类 简单使用

    在test.py文件里面 #coding=utf-8 #类的定义 class user: #定义私有属性 __name = '' __age = 0 #定义基本属性 sex = '' #定义构造函数 ...

  9. struts2实现XML异步交互

    异步交互,在不用重新提交整个页面的情况下可以实现页面局部信息与服务器的交互.在编写异步交互时需要用到一个架包:dom4j,下载地址为:https://dom4j.github.io/ 下面通过例子说明 ...

  10. Springboot杂七杂八

    1 加载配置文件 private static ResourceBundle resb = ResourceBundle.getBundle("serverInfo"); 然后在r ...