go语言json序列化

我们知道 API 格式一般以 json 格式居式,所以说下json的序列化和反序列化的使用

# json序列化和反序列化

golang标准库encoding/json是用来处理json数据的

package main
import (
"encoding/json"
"fmt"
)
type Result struct {
Code int `json:"code"`
Message string `json:"this i message"`
}
type ColorGroup struct {
    ID     int
    Name   string
    Colors []string
}
func main() {
var res Result
res.Code = 200
res.Message = "成功了"

// 序列化
jsons, err := json.Marshal(res)
if err != nil {
fmt.Println("json marshal error:", err)
}
fmt.Println("示例一:json data:", string(jsons))
fmt.Println("++++++++++++++++")

// 序列化
   group := ColorGroup{
       ID:     1,
       Name:   "Reds",
       Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
   }
   b, err := json.Marshal(group)
   if err != nil {
       fmt.Println("error:", err)
   }
   fmt.Println("示例二:json data:", string(b[:]))
   fmt.Println("++++++++++++++++")

// 反序列化
var res2 Result
err = json.Unmarshal(jsons, &res2)
if err != nil {
fmt.Println("json unmarshal error:", err)
}
fmt.Println("示例一:unjson data:",res2)
fmt.Println("++++++++++++++++")

// 反序列化
   var jsonBlob = []byte(`[
       {"ID":1,"Name":"Reds1","Colors":["Crimson","Red1","Ruby1","Maroon1"]},
       {"ID":2,"Name":"Reds2","Colors":["Crimson","Red2","Ruby2","Maroon2"]},
       {"ID":3,"Name":"Reds3","Colors":["Crimson","Red3","Ruby3","Maroon3"]}
   ]`)
   var animals []ColorGroup
   error := json.Unmarshal(jsonBlob, &animals)
   if error != nil {
       fmt.Println("error:", error)
   }
   fmt.Println("示例二:unjson data:", animals)
   fmt.Println("++++++++++++++++")
   for i, x := range animals {
       fmt.Println("示例二:unjson data:", i, x)
   }
   
   // 序列化Map和反序列化Map
fmt.Println("")
   fmt.Println("序列化Map和反序列化Map")
   r := make(map[string]interface{})
r["code"] = 200
r["msg"]  = "success"
r["data"] = map[string]interface{}{
"username" : "Tom",
"age"      : "30",
"hobby"    : []string{"读书","爬山"},
}
fmt.Println("原始Map data :", r)

//序列化
jsons, errs := json.Marshal(r)
if errs != nil {
fmt.Println("序列化Map marshal error:", errs)
}
fmt.Println("--- map to json ---")
fmt.Println("序列化Map json data :", string(jsons))

//反序列化
r2 := make(map[string]interface{})
errs = json.Unmarshal([]byte(jsons), &r2)
if errs != nil {
fmt.Println("反序列化Map json marshal error:", errs)
}
fmt.Println("--- json to map ---")
fmt.Println("序列化Map data :", r2)
}

运行结果:
C:\Go\www>go run dengyu.go
示例一:json data: {"code":200,"this i message":"成功了"}
++++++++++++++++
示例二:json data: {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroo
n"]}
++++++++++++++++
示例一:unjson data: {200 成功了}
++++++++++++++++
示例二:unjson data: [{1 Reds1 [Crimson Red1 Ruby1 Maroon1]} {2 Reds2 [Crimson R
ed2 Ruby2 Maroon2]} {3 Reds3 [Crimson Red3 Ruby3 Maroon3]}]
++++++++++++++++
示例二:unjson data: 0 {1 Reds1 [Crimson Red1 Ruby1 Maroon1]}
示例二:unjson data: 1 {2 Reds2 [Crimson Red2 Ruby2 Maroon2]}
示例二:unjson data: 2 {3 Reds3 [Crimson Red3 Ruby3 Maroon3]}

序列化Map和反序列化Map
原始Map data : map[code:200 data:map[age:30 hobby:[读书 爬山] username:Tom] msg:
success]
--- map to json ---
序列化Map json data : {"code":200,"data":{"age":"30","hobby":["读书","爬山"],"us
ername":"Tom"},"msg":"success"}
--- json to map ---
序列化Map data : map[code:200 data:map[age:30 hobby:[读书 爬山] username:Tom] ms
g:success]

(完)



(完)