Go语言之JSON序列化


  • 序列化(func Marshal(v interface{})([]byte, error) )

    • 结构体序列化
    • Map序列化
    • Tag (加了Tag,结构体就会映射你序列化以及反序列化的key)

json_demo.go文件

    package json_demo
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type Serve struct {
        ServeName string `json:"name"`         //`json:"serve_name"`  加Tag
        ServeIp   string `json:"ip"`
        ServePort int    `json:"port"`
    }
    //结构体转化为json
    func SerializeStruct() {
        serve := new(Serve) //创建
        serve.ServeName = "json-demo-struct"
        serve.ServeIp = "127.0.0.1"
        serve.ServePort = 8080
    
        b,err := json.Marshal(serve)  //序列化成json字节数组
        if err != nil {
            fmt.Println("Marshal err:", err.Error())
            return
        }
        //将json字节数组转化为string
        fmt.Println("Marshal json:",string(b))
        //结果:Marshal json: {"ServeName":"json-demo","ServeIp":"127.0.0.1","ServePort":8080}
        //加Tag:Marshal json: {"name":"json-demo-struct","ip":"127.0.0.1","port":8080}
    }
    func SerializeMap()  {
        serve := make(map[string]interface{}) //创建
        serve["ServeName"] = "json-demo-map"
        serve["ServeIp"] = "198.0.0.1"
        serve["ServePort"] = 8090
    
        b,err := json.Marshal(serve)  //序列化成json字节数组
        if err != nil {
            fmt.Println("Marshal err:", err.Error())
            return
        }
        //将json字节数组转化为string
        fmt.Println("Marshal json:",string(b))
        //结果:Marshal json: {"ServeIp":"198.0.0.1","ServeName":"json-demo-map","ServePort":8090}
    }

main.go文件

    package main
    
    import "./json_demo"
    func main() {
        json_demo.SerializeStruct()
        json_demo.SerializeMap()
    }

  • 反序列化(func Unmarshal(data []byte, v interface{}) error)

    • 反序列化为结构体
    • 反序列化为Map

json_demo.go文件

    package json_demo
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type Serve struct {
        ServeName string `json:"name"`         //`json:"serve_name"`  加Tag
        ServeIp   string `json:"ip"`
        ServePort int    `json:"port"`
    }
    //json转化为结构体
    func DeSerializeStruct() {
        jsonString := `{"ip":"198.0.0.1","name":"json-demo-map","port":8090}`
        serve := new(Serve)
        err := json.Unmarshal([]byte(jsonString),&serve)
        if err != nil{
            fmt.Println("Unmarshal err:", err.Error())
            return
        }
        fmt.Println("Unmarshal struct:", serve) //Unmarshal struct: &{json-demo-map 198.0.0.1 8090}
    }
    //json转化为map
    func DeSerializeMap() {
        jsonString := `{"ServeIp":"198.0.0.1","ServeName":"json-demo-map","ServePort":8090}`
        serve := make(map[string]interface{})
        err := json.Unmarshal([]byte(jsonString),&serve)
        if err != nil{
            fmt.Println("Unmarshal err:", err.Error())
            return
        }
        fmt.Println("Unmarshal map:", serve) //Unmarshal map: map[ServeIp:198.0.0.1 ServeName:json-demo-map ServePort:8090]
    }

main.go文件

    package main
    
    import "./json_demo"
    func main() {
        json_demo.DeSerializeStruct()
        json_demo.DeSerializeMap()
    }

关注友儿不迷路

Last modification:September 24th, 2020 at 11:36 am
如果觉得我的文章对你有用,请随意赞赏