網站首頁 編程語言 正文
前言
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
相關推薦
- 2022-05-22 云原生Kubernetes初始化容器Init使用教程_云其它
- 2022-06-21 C++超詳細講解友元的使用_C 語言
- 2022-04-08 go實現圖片拼接與文字書寫的方法實例_Golang
- 2022-10-29 Android 開發 | API 指南- Content Provider 應用程序的使用方法
- 2022-07-17 SQL實現查詢某字段的值為空的記錄_MsSql
- 2024-01-29 使用 Maven 進行項目構建:深入了解 `mvn clean install -U` 命令
- 2022-04-07 Go語言中的Base64編碼原理介紹以及使用_Golang
- 2022-03-31 python猜單詞游戲的實現_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支