網(wǎng)站首頁 編程語言 正文
前言
Go語言的序列化與反序列化在工作中十分常用,在Go語言中提供了相關(guān)的解析方法去解析JSON,操作也比較簡單
序列化
// 數(shù)據(jù)序列化 func Serialize(v interface{})([]byte, error) // fix參數(shù)用于添加前綴 //idt參數(shù)用于指定你想要縮進的方式 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中,我們會先去檢測傳進來對象是否為內(nèi)置類型,是則編碼,不是則會先檢查當前對象是否已經(jīng)實現(xiàn)了Marshaler接口,實現(xiàn)則執(zhí)行MarshalJSON方法得到自定義序列化后的數(shù)據(jù),沒有則繼續(xù)檢查是否實現(xiàn)了TextMarshaler接口,實現(xiàn)的話則執(zhí)行MarshalText方法對數(shù)據(jù)進行序列化
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
該函數(shù)會把傳入的數(shù)據(jù)作為json解析,然后把解析完的數(shù)據(jù)存在arbitrarily中,arbitrarily是任意類型的參數(shù),我們在用此函數(shù)進行解析時,并不知道傳入?yún)?shù)類型所以它可以接收所有類型且它一定是一個類型的指針
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) }
總結(jié)
原文鏈接:https://juejin.cn/post/7122270032448454664
相關(guān)推薦
- 2022-09-20 RecyclerView實現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點續(xù)傳_Android
- 2023-07-07 sklearn.model_selection模塊介紹
- 2021-12-06 樹莓派4B+EdgeX+MQTT的填坑之旅
- 2023-03-20 淺談Redis在秒殺場景的作用_Redis
- 2022-08-04 基于Python實現(xiàn)煙花效果的示例代碼_python
- 2022-09-29 React事件監(jiān)聽和State狀態(tài)修改方式_React
- 2022-07-27 C++中整形與浮點型如何在內(nèi)存中的存儲詳解_C 語言
- 2024-01-14 “xxx“ is not an enclosing class 解決辦法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支