Golang實現(xiàn)結(jié)構(gòu)體和Json格式數(shù)據(jù)之間的互相轉(zhuǎn)換
摘要
本節(jié)主要學(xué)習(xí)Golang結(jié)構(gòu)體和JSON序列化數(shù)據(jù)的轉(zhuǎn)換命令。
1. 結(jié)構(gòu)體到j(luò)son格式
1.1 簡單轉(zhuǎn)換
Golang結(jié)構(gòu)體轉(zhuǎn)換成JSON格式數(shù)據(jù),主要在結(jié)構(gòu)體的相關(guān)字段中加入json : "keyword"字段。具體做法如下:
type Structname struct{
feild1 Type1 `json:"keyword1"`
feild2 Type2 `json:"keyword2"`
}
相關(guān)具體實例如下:
package message
import (
"encoding/json"
"log"
"testing"
)
type Information struct{
Name string `json:"name"`
Addr string `json:"addr"`
}
func TestStructure(t *testing.T){
var inf Information
inf.Name="Alice"
inf.Addr="Green Street"
data,err:=json.Marshal(inf)
if err!=nil{
panic(err)
}
log.Println(string(data))
}
1.2 遞歸轉(zhuǎn)換
為了轉(zhuǎn)換一個嵌套結(jié)構(gòu)體為JSON格式文件,首先在需要轉(zhuǎn)換的結(jié)構(gòu)體中構(gòu)建json:"keyword"字段,其次,構(gòu)建一個嵌套的Golang結(jié)構(gòu)體,最后利用Json.Marshal函數(shù)進行轉(zhuǎn)換。
package message
import (
"encoding/json"
"errors"
"log"
"testing"
)
type Employee struct {
Position string `json:"position"`
Name Name `json:"name"`
}
type Name struct {
FirstName string `json:"firstname"`
Surname string `json:"surname"`
}
func TestStructure(t *testing.T){
name:=Name{FirstName:"Zhang",Surname:"san"}
employee:=Employee{Position:"China",Name: name}
person,err:=json.Marshal(employee)
if err!=nil{
panic(errors.New("Wrong Converting behavior"))
}
log.Println(string(person))
}
2. json格式到結(jié)構(gòu)體
2.1 簡單轉(zhuǎn)換
JSON格式數(shù)據(jù)轉(zhuǎn)換為Golang結(jié)構(gòu)體的過程,利用Json.Ummarshal函數(shù)進行轉(zhuǎn)換,具體轉(zhuǎn)換例子如下:
package message
import (
"encoding/json"
"log"
"testing"
)
type Information struct{
Name string `json:"name"`
Addr string `json:"addr"`
}
func TestStructure(t *testing.T){
alice:="{\"name\":\"Alice\",\"addr\":\"Green Street\"}"
inf:=new(Information)
err:=json.Unmarshal([]byte(alice),inf)
if err!=nil{
panic(err)
}
log.Println(inf)
}
2.2 嵌套JSON格式數(shù)據(jù)轉(zhuǎn)換
類似于嵌套結(jié)構(gòu)體轉(zhuǎn)換到JSON格式的過程,從JSON格式數(shù)據(jù)轉(zhuǎn)換到嵌套結(jié)構(gòu)體數(shù)據(jù)的過程,就是首先構(gòu)建嵌套JSON格式數(shù)據(jù),其次通過Json.Ummarshal函數(shù)轉(zhuǎn)換為嵌套結(jié)構(gòu)體數(shù)據(jù)。
package message
import (
"encoding/json"
"log"
"testing"
)
type Employee struct {
Position string `json:"position"`
Name Name `json:"name"`
}
type Name struct {
FirstName string `json:"firstname"`
Surname string `json:"surname"`
}
func TestStructure(t *testing.T){
str:="{\"position\":\"China\",\"name\":{\"firstname\":\"Zhang\",\"surname\":\"san\"}}"
person1:=new(Employee)
json.Unmarshal([]byte(str),person1)
log.Println(person1,person1.Name)
}
以上就是JSon格式數(shù)據(jù)和Golang結(jié)構(gòu)體之間的數(shù)據(jù)轉(zhuǎn)換過程。
到此這篇關(guān)于Golang實現(xiàn)結(jié)構(gòu)體和Json格式數(shù)據(jù)之間的互相轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Golang結(jié)構(gòu)體和Json互轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go調(diào)度器學(xué)習(xí)之goroutine調(diào)度詳解
這篇文章主要為大家詳細介紹了Go調(diào)度器中g(shù)oroutine調(diào)度的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
Golang編程并發(fā)工具庫MapReduce使用實踐
這篇文章主要為大家介紹了Golang并發(fā)工具庫MapReduce的使用實踐,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
手把手教你vscode配置golang開發(fā)環(huán)境的步驟
這篇文章主要介紹了手把手教你vscode配置golang開發(fā)環(huán)境的步驟,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

