Golang實(shí)現(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 簡(jiǎn)單轉(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)具體實(shí)例如下:
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)換一個(gè)嵌套結(jié)構(gòu)體為JSON格式文件,首先在需要轉(zhuǎn)換的結(jié)構(gòu)體中構(gòu)建json:"keyword"字段,其次,構(gòu)建一個(gè)嵌套的Golang結(jié)構(gòu)體,最后利用Json.Marshal函數(shù)進(jìn)行轉(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 簡(jiǎn)單轉(zhuǎn)換
JSON格式數(shù)據(jù)轉(zhuǎn)換為Golang結(jié)構(gòu)體的過(guò)程,利用Json.Ummarshal函數(shù)進(jìn)行轉(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)換
類(lèi)似于嵌套結(jié)構(gòu)體轉(zhuǎn)換到JSON格式的過(guò)程,從JSON格式數(shù)據(jù)轉(zhuǎn)換到嵌套結(jié)構(gòu)體數(shù)據(jù)的過(guò)程,就是首先構(gòu)建嵌套JSON格式數(shù)據(jù),其次通過(guò)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ò)程。
到此這篇關(guān)于Golang實(shí)現(xiàn)結(jié)構(gòu)體和Json格式數(shù)據(jù)之間的互相轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Golang結(jié)構(gòu)體和Json互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Golang實(shí)現(xiàn)自定義時(shí)間結(jié)構(gòu)體并支持Json&Gorm
- Golang實(shí)現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體的示例詳解
- 詳解Go語(yǔ)言中結(jié)構(gòu)體與JSON間的轉(zhuǎn)換
- go語(yǔ)言通過(guò)結(jié)構(gòu)體生成json示例解析
- Go 結(jié)構(gòu)體、數(shù)組、字典和 json 字符串的相互轉(zhuǎn)換方法
- go語(yǔ)言使用第三方包 json化結(jié)構(gòu)體操作示例
- golang結(jié)構(gòu)體與json格式串實(shí)例代碼
相關(guān)文章
詳解Go語(yǔ)言如何判斷兩個(gè)對(duì)象是否相等
在編程中,判斷兩個(gè)對(duì)象是否相等是一項(xiàng)常見(jiàn)的任務(wù),同時(shí)判斷對(duì)象是否相等在很多情況下都非常重要,所以在接下來(lái)的內(nèi)容中,我們將詳細(xì)介紹在?Go?語(yǔ)言中如何判斷對(duì)象是否相等的方法和技巧,需要的可以參考一下2023-06-06
Go并發(fā)編程實(shí)現(xiàn)數(shù)據(jù)競(jìng)爭(zhēng)
本文主要介紹了Go并發(fā)編程實(shí)現(xiàn)數(shù)據(jù)競(jìng)爭(zhēng),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Go語(yǔ)言數(shù)據(jù)類(lèi)型簡(jiǎn)單介紹
這篇文章主要介紹了Go語(yǔ)言數(shù)據(jù)類(lèi)型簡(jiǎn)單介紹的相關(guān)資料,需要的朋友可以參考下2023-08-08
Go?for-range?的?value值地址每次都一樣的原因解析
循環(huán)語(yǔ)句是一種常用的控制結(jié)構(gòu),在?Go?語(yǔ)言中,除了?for?關(guān)鍵字以外,還有一個(gè)?range?關(guān)鍵字,可以使用?for-range?循環(huán)迭代數(shù)組、切片、字符串、map?和?channel?這些數(shù)據(jù)類(lèi)型,這篇文章主要介紹了Go?for-range?的?value值地址每次都一樣的原因解析,需要的朋友可以參考下2023-05-05

