A, general

JSON(JavaScript Object Notation) is a lightweight data interchange format that is now the dominant data format used in Web development as a way to interact data between the front end and the back end

Second, ingoSeveral of the common serialization intojsonThe way of

  • 1. Serialize the structure to JSON

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type Student struct {
        Name   string   `json:"name"`   / / name
        Age    int      `json:"age"`    / / age
        Gender string   `json:"gender"` / / gender
        Score  float64  `json:"score"`  / / score
        Course []string `json:"course"` / / course
    }
    
    func main(a) {
        stu := Student{
            "Zhang".20."Male".78.6And []string{"Chinese"."Mathematics"."Music"},
        }
        fmt.Println(stu)
        data, err := json.Marshal(&stu)
        iferr ! =nil {
            fmt.Println("Serialization error", err)
        } else {
            fmt.Println(string(data))
        }
    }
    / / return the result: {" name ":" zhang ", "age" : 20, "gender" : "male" and "score" : 78.6, "course" : [" language ", "mathematics", "music"]}
    Copy the code
  • 2. Serialize the map to JSON

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main(a) {
        var map1 map[string]interface{} // Use an empty interface to represent any type
        map1 = make(map[string]interface{})
        map1["name"] = "Zhang"
        map1["province"] = "Guangdong Province"
        map1["city"] = "Shenzhen"
        map1["salary"] = 1000
        map1["hobby"] = [...].string{"Reading"."Travel"."Learning"}
    
        data, err := json.Marshal(map1)
        iferr ! =nil {
            fmt.Println("Serialization failed")}else {
            fmt.Println(string(data))
        }
    }
    Copy the code
  • 3. Serialize slices to JSON

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main(a) {
        var slice1 []map[string]interface{}
        var m1 map[string]interface{}
        m1 = make(map[string]interface{})
        m1["name"] = "Zhang"
        m1["address"] = "Shenzhen City, Guangdong Province"
        slice1 = append(slice1, m1)
    
        var m2 map[string]interface{}
        m2 = make(map[string]interface{})
        m2["name"] = "Bill"
        m2["address"] = "Guangzhou, Guangdong Province"
        slice1 = append(slice1, m2)
    
        data, err := json.Marshal(slice1)
        iferr ! =nil {
            fmt.Println("Serialization error")}else {
            fmt.Println(string(data))
        }
    }
    Copy the code

3. Deserialization (willjsonconvertgoStructures recognized by the code,map, slicing)

  • 1. Deserialize json to a structure

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type Student struct {
        Name   string   `json:"name"`   / / name
        Age    int      `json:"age"`    / / age
        Gender string   `json:"gender"` / / gender
        Score  float64  `json:"score"`  / / score
        Course []string `json:"course"` / / course
    }
    
    func main(a) {
        var stu Student
        str := ` {" name ":" zhang ", "age" : 20, "gender" : "male" and "score" : 78.6, "course" : [" language ", "mathematics", "music"]} `
    
        err := json.Unmarshal([]byte(str), &stu)
        iferr ! =nil {
            fmt.Println("Deserialization failed")
        }
        fmt.Println(stu)
    }
    Copy the code
  • 2. Deserialize json to map

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main(a) {
        var map1 map[string]interface{} // Use an empty interface to represent any type
        //map1 = make(map[string]interface{})
        str := ` {" city ", "shenzhen", "hobby" : [" reading ", "travel", "learning"], "name" : "zhang", "province", "guangdong province", "salary" : 1000} `
    
        err := json.Unmarshal([]byte(str), &map1)
        iferr ! =nil {
            fmt.Println("Deserialization failed", err)
        }
        fmt.Println(map1)
    }
    Copy the code
  • 3. Deserialize slices

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main(a) {
        var slice []map[string]interface{}
        str := ` [{" address ", "shenzhen city, guangdong province", "name" : "* *"}, {" address ", "guangzhou city, guangdong province", "name" : "bill"}] `
        err := json.Unmarshal([]byte(str), &slice)
        iferr ! =nil {
            fmt.Printf("unmarshal err=%v\n", err) }
        fmt.Printf("Slice =%v\n after deserialization", slice)
    }
    Copy the code