json序列化和反序列化

栏目: 后端 · 前端 · 发布时间: 4年前

内容简介:对于json的序列化和反序列化,go的encoding/json 包提供了一些列的方法。 常用的比如

序列化和反序列化

1、JSON的序列化

1.1序列化 struct、map、slice

对于json的序列化和反序列化,go的encoding/json 包提供了一些列的方法。 常用的比如

func Marshal(v interface{}) ([]byte, error) 序列化

func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) 同Marshal,并且可以格式化

第一个参数是要序列化的数据,第二个是每行的前缀,第三个是每行的缩进,比如MarshalIndent(map,""," ")

json-iterator ,有 javago 两种实现

序列化练习:

package main

import (
    "encoding/json"
    "fmt"
)

type  Student struct {
    Name string
    Age  int
    Height float32
    isMarried bool
}

func main(){
    testStruct()
    testMap()
    testSlice()
}
func testStruct(){
    student:=Student{
        Name:"张三",
        Age:18,
        Height:178.12,
        isMarried:false,

    }
    bytes, err := json.Marshal(student)
    if err!=nil{
        fmt.Println("序列化错误")
    }
    fmt.Printf("序列化struct    %v\n",string(bytes))
}
func testMap(){
    m:=make(map[string]interface{})
    m["result"]="1"
    m["message"]="success"
    m["data"]=Student{"孙悟空",10000,160.50,false}
    if bytes, e := json.Marshal(m);e==nil{
        fmt.Printf("序列化map    %v\n",string(bytes))
    }else{
        fmt.Println("序列化错误")
    }
}
func testSlice(){
    m:=make(map[string]interface{})
    s:=[]Student{Student{"孙悟空",10000,160.50,false},Student{"猪八戒",10000,180.50,true}}
    m["result"]="1"
    m["message"]="success"
    m["data"]=s
    if bytes, e := json.Marshal(m);e==nil{
        fmt.Printf("序列化slice    %v\n",string(bytes))
    }else{
        fmt.Println("序列化错误")
    }
}
//结果
序列化struct    {"Name":"张三","Age":18,"Height":178.12}
序列化map    {"data":{"Name":"孙悟空","Age":10000,"Height":160.5},"message":"success","result":"1"}
序列化slice    {"data":[{"Name":"孙悟空","Age":10000,"Height":160.5},{"Name":"猪八戒","Age":10000,"Height":180.5}],"message":"success","result":"1"}

1.2 自定义序列化后json key

json包通过反射机制序列化,我们可以通过添加struct 的tag,自定义返回json 的key

只需要添加一个 json:"key" 这种方式就可以

package main

import (
    "encoding/json"
    "fmt"
)

type  Student struct {
    Name string    `json:"newName"`     //tag标签、反射机制
    Age  int    `json:"newAge"`
    Height float32
    isMarried bool
}
func main(){
    testSlice()
}
func testSlice(){
    m:=make(map[string]interface{})
    s:=[]Student{Student{"孙悟空",10000,160.50,false},Student{"猪八戒",10000,180.50,true}}
    m["result"]="1"
    m["message"]="success"
    m["data"]=s
    if bytes, e := json.Marshal(m);e==nil{
        fmt.Printf("序列化slice    %v\n",string(bytes))
    }else{
        fmt.Println("序列化错误")
    }
}
//序列化slice    {"data":[{"newName":"孙悟空","newAge":10000,"Height":160.5},{"newName":"猪八戒","newAge":10000,"Height":180.5}],"message":"success","result":"1"}

2 json反序列化

func Unmarshal(data []byte, v interface{}) error 反序列化,接收一个byte数组和要解析的类型,返回error,如果解析成功返回nil。使用时注意,无需实例化,Unmarshal内部实现了,比如map ,struct .

//后两个配合使用,比如接收网络返回结果:err = json.NewDecoder(resp.Body).Decode(&gr)

func NewDecoder(r io.Reader) *Decoder

func (dec *Decoder) Decode(v interface{}) error

用这个函数就够了,看代码

package main

import (
    "encoding/json"
    "fmt"
)

/*
序列化struct    {"Name":"张三","Age":18,"Height":178.12}
序列化map    {"data":{"Name":"孙悟空","Age":10000,"Height":160.5},"message":"success","result":"1"}
序列化slice    {"data":[{"Name":"孙悟空","Age":10000,"Height":160.5},{"Name":"猪八戒","Age":10000,"Height":180.5}],"message":"success","result":"1"}
 */

type Student struct {
    Name      string
    Age       int
    Height    float32
    isMarried bool
}

func main() {
    testStruct()
    testMap()
    testSlice()
}

func testStruct() {
    strStruct := "{\"Name\":\"张三\",\"Age\":18,\"Height\":178.12}"
    var std Student  //无需实例化,Unmarshal内部实现了
    err := json.Unmarshal([]byte(strStruct), &std)
    if err != nil {
        fmt.Println("testStruct反序列化失败",err)
    }
    fmt.Printf("反序列化json结果:%+v\n", std)
}

func testMap(){
    str:="{\"data\":{\"Name\":\"孙悟空\",\"Age\":10000,\"Height\":160.5},\"message\":\"success\",\"result\":\"1\"}"
    var m map[string]interface{}
    err := json.Unmarshal([]byte(str), &m)
    if err != nil {
        fmt.Println("testMap反序列化失败",err)
    }
    fmt.Printf("反序列化json结果:%v\n", m)
}

func  testSlice(){
    str:="[{\"Name\":\"孙悟空\",\"Age\":10000,\"Height\":160.5},{\"Name\":\"猪八戒\",\"Age\":10000,\"Height\":180.5}]"
    var  sli []Student
    err := json.Unmarshal([]byte(str), &sli)
    if err != nil {
        fmt.Println("testSlice反序列化失败",err)
    }
    fmt.Printf("反序列化json结果:%+v\n", sli)
}

3、第三方库

1 encoding/json Golang原生
2 easyjson https://github.com/mailru/eas...
4 iterator/json https://github.com/json-itera...

除了我们上面的原生标准库的 encoding/json ,我推荐使用下面两款第三方库,性能都很高。

iterator对动态支持更好。具体使用看官方文档。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机系统基础

计算机系统基础

袁春风 / 机械工业出版社 / 2014-7-1 / CNY 49.00

《计算机类专业系统能力培养系列教材:计算机系统基础》主要介绍与计算机系统相关的核心概念,解释这些概念如何相互关联并最终影响程序执行的结果和性能。共分8章,主要内容包括数据的表示和运算、程序的转换及机器级表示、程序的链接、程序的执行、存储器层次结构、虚拟存储器、异常控制流和I/O操作的实现等。内容详尽,反映现实,概念清楚,通俗易懂,实例丰富,并提供大量典型习题供读者练习。本书可以作为计算机专业本科或......一起来看看 《计算机系统基础》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具