• [转] golang中struct、json、map互相转化


    一、Json和struct互换

    (1)Json转struct例子:

    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的首字母必须大写,而json中大小写都可以。

    (2)struct转json

            在结构体中引入tag标签,这样匹配的时候json串对应的字段名需要与tag标签中定义的字段名匹配,当然tag中定义的名称不需要首字母大写,且对应的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()
    }

    输出:

     

    二、json和map互转

    (1)json转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转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))
    }

    输出:

     

    三、map和struct互转

    (1)map转struct

    需要安装一个第三方库
    在命令行中运行: 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转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)
    }

    输出:


    ---------------------
    作者:小拳头
    来源:CSDN
    原文:https://blog.csdn.net/xiaoquantouer/article/details/80233177
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    通过应用程序域AppDomain加载和卸载程序集(转自张逸)
    Asp.net 2.0 中获取控件输出的Html代码 (转)
    工作和学习
    查询数据库中的所有表
    观活动板房感怀
    温心之旅
    读书无用论是21世纪最大的阴谋
    我渴望女人,但我更渴望成功
    外面比家里亮
    近来,可好?
  • 原文地址:https://www.cnblogs.com/fanbi/p/10736320.html
Copyright © 2020-2023  润新知