Go语言中序列化与反序列化示例详解
作者:前端若水 发布时间:2024-02-10 11:43:57
标签:go,反序列化,序列化
前言
Go语言的序列化与反序列化在工作中十分常用,在Go语言中提供了相关的解析方法去解析JSON,操作也比较简单
序列化
// 数据序列化
func Serialize(v interface{})([]byte, error)
// fix参数用于添加前缀
//idt参数用于指定你想要缩进的方式
func serialization (v interface{}, fix, idt string) ([]byte, error)
array、slice、map、struct对象
//struct
import (
"encoding/json"
"fmt"
)
type Student struct {
Id int64
Name string
Desc string
}
func fn() {
std := &Student{0, "Ruoshui", "this to Go"}
data, err := json.MarshalIndent(std, "", " ")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(data))
}
//array、slice、map
func fc() {
s := []string{"Go", "java", "php"}
d, _ := json.MarshalIndent(s, "", "\t")
fmt.Println(string(d))
m := map[string]string{
"id": "0",
"name":"ruoshui",
"des": "this to Go",
}
bytes, _ := json.Marshal(m)
fmt.Println(string(bytes))
}
序列化的接口
在json.Marshal中,我们会先去检测传进来对象是否为内置类型,是则编码,不是则会先检查当前对象是否已经实现了Marshaler接口,实现则执行MarshalJSON方法得到自定义序列化后的数据,没有则继续检查是否实现了TextMarshaler接口,实现的话则执行MarshalText方法对数据进行序列化
MarshalJSON与MarshalText方法虽然返回的都是字符串,不过MarshalJSON方法返回的带引号,MarshalText方法返回的不带引号
//返回带引号字符串
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
type Unmarshaler interface {
UnmarshalJSON([]byte) error
}
//返回不带引号的字符串
type TextMarshaler interface {
MarshalText() (text []byte, err error)
}
type TextUnmarshaler interface {
UnmarshalText(text []byte) error
}
反序列化
func Unmarshal(data [] byte, arbitrarily interface{}) error
该函数会把传入的数据作为json解析,然后把解析完的数据存在arbitrarily中,arbitrarily是任意类型的参数,我们在用此函数进行解析时,并不知道传入参数类型所以它可以接收所有类型且它一定是一个类型的指针
slice、map、struct反序列化
//struct
type Student struct {
Id int64 `json:"id,string"`
Name string `json:"name,omitempty"`
Desc string `json:"desc"`
}
func fn() {
str := `{"id":"0", "name":"ruoshui", "desc":"new Std"}`
var st Student
_ = json.Unmarshal([]byte(str), &st)
fmt.Println(st)
}
//slice和map
func f() {
slice := `["java", "php", "go"]`
var sli []string
_ = json.Unmarshal([]byte(slice), &sli)
fmt.Println(sli)
mapStr := `{"a":"java", "b":"node", "c":"php"}`
var newMap map[string]string
_ = json.Unmarshal([]byte(mapStr), &newMap)
fmt.Println(newMap)
}
来源:https://juejin.cn/post/7122270032448454664
0
投稿
猜你喜欢
- 批标准化层 tf.keras.layers.Batchnormalization()tf.keras.layers.Batchnormali
- Dataset类是TensorFlow非常流行的存储数据的格式。常用来作为输入输出。data模块主要的用途就是通过这种方法创建Dataset
- Python中datetime库的用法datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和M
- 1. 手动操作1.1. 显示模块pip list1.2. 显示过期模块pip list --outdated1.3. 安装模块pip ins
- 引言Python 是一个强大的语言,提供了许多内置函数以帮助开发者编写高效、简洁的代码。在这篇文章中,我们将深入探讨三个内置函数:map、f
- 实现爬虫的增量式爬取有两种方法,一是在获得页面解析的内容后判断该内容是否已经被爬取过,二是在发送请求之前判断要被请求的url是否已经被爬取过
- 列举了一些常见,新手经常问的问题。举例并说明解决方法。1.超链接访问过后hover样式就不出现的问题运行代码框<!DOCTYPE ht
- 一、在CentOS上安装Python31.下载Python3.10源代码文件下载地址:https://www.python.org/down
- Web,全称为 World Wide Web,是 Internet 上最重要和最为人们所熟知的应用之一。Web 是指 Internet 上所
- 微信小程序与php 实现微信支付的简单实例小程序访问地址:payfee.PHP:include 'WeixinPay.php'
- PHP mysqli_stmt_init() 函数初始化声明并返回 mysqli_stmt_prepare() 使用的对象:<?php
- WordPress默认登录页面:http://192.168.0.120/wordpress/wp-login.php 1、Chrome浏览
- 我就废话不多说了,大家还是直接看代码吧~# -*- coding: utf-8 -*-#keras==2.0.5#tensorflow==1
- 1.int,float相互转换例1:int转float使用float(int)float转int使用int(float)# coding:u
- 小贤是一条可爱的小狗(Dog), 它的叫声很好听(wow), 每次看到主人的时候就会乖乖叫一声(yelp).从这段描述可以得到以下对象:fu
- Python 字符串格式化使用 "字符 %格式1 %格式2 字符"%(变量1,变量2),%格式表示接受变量的类型。简单的
- 技术背景在前面一篇博客中我们介绍过关于python的表格数据处理方案,这其中的工作重点就是对表格类型的数据进行梳理、计算和展示,本文重点介绍
- 这是一张灵异事件图。。。开个玩笑,这就是一张普通的图片。毫无疑问,上面的那副图画看起来像一幅电脑背景图片。这些都归功于我的妹妹,她能够将一些
- 问:假如我的一个表里含有(a,b,c,d)和(a,b)形成组合键。我能在列值中写这个查询吗?例如: select a,c,d from my
- 前言为了保证数据的一致完整性,任何一个数据库都存在锁定机制。锁定机制的优劣直接应想到一个数据库系统的并发处理能力和性能,所以锁定机制的实现也