golang struct json map mutual conversion

table of Contents

One, Json and struct interchange

(1) Json to struct example

(2) struct to json

Two, json and map mutual conversion

(1) json to map example

(2) Map to Json example

Three, map and struct mutual conversion

(1) Map to struct

(2) Example of struct to map


One, Json and struct interchange

(1) Json to struct example

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

Output:

{0}

Note that the key in json must be consistent with the key in struct. The first letter of the key in struct must be capitalized, and upper and lower case in json can be used.

(2) struct to json

        Introduce the tag tag in the structure, so that when matching, the field name corresponding to the json string needs to match the field name defined in the tag tag. Of course, the name defined in the tag does not need to be capitalized, and the field name in the corresponding json string is still Not case sensitive. At this time, the corresponding field name in the structure does not need to be consistent with the matched one, but the first letter must be capitalized, and only capitalized can provide external access.

example:

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

Output:

Two, json and map mutual conversion

(1) json to map example

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

Output:

(2) Map to Json example

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

Output:

Example 2:

func MapToJsonDemo2(){
        b, _ := json.Marshal(map[string]int{"test":1, "try":2})
        fmt.Println(string(b))
}

Output:

Three, map and struct mutual conversion

(1) Map to struct

Need to install a third-party library

Run in the command line: go get github.com/goinggo/mapstructure

example:

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

Output

(2) Example of struct to 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)
}

Output:

 

 

Guess you like

Origin blog.csdn.net/whatday/article/details/115200602