zoukankan      html  css  js  c++  java
  • golang中struct、json、map互相转化

    Json转struct例子:

    注意json里面的key和struct里面的key要一致,struct中的key的首字母必须大写,而json中大小写都可以。

    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()
    }

    struct转json

    在结构体中引入tag标签,这样匹配的时候json串对应的字段名需要与tag标签中定义的字段名匹配,当然tag中定义的名称不需要首字母大写,且对应的json串中字段名仍然大小写不敏感。此时,结构体中对应的字段名可以不用和匹配的一致,但是首字母必须大写,只有大写才是可对外提供访问的。

    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()
    }

    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)
    }

    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))
    }
    func MapToJsonDemo2(){
            b, _ := json.Marshal(map[string]int{"test":1, "try":2})
            fmt.Println(string(b))
    }

    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)
    }

    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)
    }
  • 相关阅读:
    对数值计算numpy的一些总结,感兴趣的朋友可以看看
    mysql基础语法(部分)
    python_内建结构
    07_go语言基础
    06_go语言基础
    05_go语言基础常量
    04_go语言基础
    03_go语言基础
    02_go语言基础
    01_go语言基础
  • 原文地址:https://www.cnblogs.com/rxbook/p/15163771.html
Copyright © 2011-2022 走看看