golang 實(shí)現(xiàn)struct、json、map互相轉(zhuǎn)化
(1)Json轉(zhuǎn)struct例子:
package main import ( 'fmt' 'encoding/json') type People struct { Name string `json:'name_title'` Age int `json:'age_size'`} func JsonToStructDemo(){ jsonStr := ` {'name_title': 'jqw''age_size':12 } ` var people People json.Unmarshal([]byte(jsonStr), &people) fmt.Println(people)} func main(){ JsonToStructDemo()}
輸出:
注意json里面的key和struct里面的key要一致,struct中的key的首字母必須大寫(xiě),而json中大小寫(xiě)都可以。
(2)struct轉(zhuǎn)json
在結(jié)構(gòu)體中引入tag標(biāo)簽,這樣匹配的時(shí)候json串對(duì)應(yīng)的字段名需要與tag標(biāo)簽中定義的字段名匹配,當(dāng)然tag中定義的名稱不需要首字母大寫(xiě),且對(duì)應(yīng)的json串中字段名仍然大小寫(xiě)不敏感。此時(shí),結(jié)構(gòu)體中對(duì)應(yīng)的字段名可以不用和匹配的一致,但是首字母必須大寫(xiě),只有大寫(xiě)才是可對(duì)外提供訪問(wèn)的。
例子:
package main import ( 'fmt' 'encoding/json') type People struct { Name string `json:'name_title'` Age int `json:'age_size'`} func StructToJsonDemo(){ p := People{Name: 'jqw',Age: 18, } jsonBytes, err := json.Marshal(p) if err != nil {fmt.Println(err) } fmt.Println(string(jsonBytes))} func main(){ StructToJsonDemo()}
輸出:
(1)json轉(zhuǎn)map例子:
func JsonToMapDemo(){ jsonStr := ` {'name': 'jqw','age': 18 } ` var mapResult map[string]interface{} err := json.Unmarshal([]byte(jsonStr), &mapResult) if err != nil {fmt.Println('JsonToMapDemo err: ', err) } fmt.Println(mapResult)}
輸出:
(2)map轉(zhuǎn)Json例子
func MapToJsonDemo1(){ mapInstances := []map[string]interface{}{} instance_1 := map[string]interface{}{'name': 'John', 'age': 10} instance_2 := map[string]interface{}{'name': 'Alex', 'age': 12} mapInstances = append(mapInstances, instance_1, instance_2) jsonStr, err := json.Marshal(mapInstances) if err != nil {fmt.Println('MapToJsonDemo err: ', err) } fmt.Println(string(jsonStr))}
輸出:
例2:
func MapToJsonDemo2(){ b, _ := json.Marshal(map[string]int{'test':1, 'try':2}) fmt.Println(string(b))}
輸出:
(1)map轉(zhuǎn)struct
需要安裝一個(gè)第三方庫(kù)
在命令行中運(yùn)行:
go get github.com/goinggo/mapstructure
例子:
func MapToStructDemo(){ mapInstance := make(map[string]interface{}) mapInstance['Name'] = 'jqw' mapInstance['Age'] = 18 var people People err := mapstructure.Decode(mapInstance, &people) if err != nil {fmt.Println(err) } fmt.Println(people)}
輸出
(2)struct轉(zhuǎn)map例子
func StructToMapDemo(obj interface{}) map[string]interface{}{ obj1 := reflect.TypeOf(obj) obj2 := reflect.ValueOf(obj) var data = make(map[string]interface{}) for i := 0; i < obj1.NumField(); i++ {data[obj1.Field(i).Name] = obj2.Field(i).Interface() } return data}func TestStructToMap(){ student := Student{10, 'jqw', 18} data := StructToMapDemo(student) fmt.Println(data)}
輸出:
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. chat.asp聊天程序的編寫(xiě)方法3. CSS 使用Sprites技術(shù)實(shí)現(xiàn)圓角效果4. phpstudy apache開(kāi)啟ssi使用詳解5. 詳解瀏覽器的緩存機(jī)制6. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法7. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?8. HTML中的XML數(shù)據(jù)島記錄編輯與添加9. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法10. 推薦一個(gè)好看Table表格的css樣式代碼詳解
