GoLang Json数据的的序列化与反序列化

序列化

json.Marshal(要序列化的对象)

 

Struct(结构体) → Json

复制代码
    //结构体序列化为json
    p := &People{
        Name: "张三",
        Age:  16,
    }
    StructJSON, err := json.Marshal(p)
    if err != nil {
        fmt.Println("结构体序列化为json失败...", err)
        return
    }
    fmt.Println("↓结构体序列化为json后:↓")
    fmt.Println(string(StructJSON))
    fmt.Println()
复制代码

 

 

Map集合(key,value) → Json

复制代码
//map集合序列化为json
    MapJSON := make(map[string]interface{})
    MapJSON["name"] = "独眼蝙蝠"
    MapJSON["lv"] = 1
    MapJSON["hp"] = 100

    mapJSONRes, err2 := json.Marshal(MapJSON)
    if err2 != nil {
        fmt.Println("map集合序列化为json失败...", err2)
        return
    }
    fmt.Println("↓map集合序列化为json后:↓")
    fmt.Println(string(mapJSONRes))
    fmt.Println()
复制代码

 

 

Map切片(多个map(key,value)) → Json

复制代码
//map切片序列化为json
    MapJSONSlice := make([]map[string]interface{}, 2)
    if MapJSONSlice[0] == nil {
        MapJSONSlice[0] = make(map[string]interface{})
        MapJSONSlice[0]["name"] = "独眼蝙蝠"
        MapJSONSlice[0]["lv"] = 1
        MapJSONSlice[0]["hp"] = 100
        MapJSONSlice[0]["goods"] = []string{"棒棒糖(HP恢复60)", "木剑(攻击力+3)", "四点色子"}
    }
    if MapJSONSlice[1] == nil {
        MapJSONSlice[1] = make(map[string]interface{})
        MapJSONSlice[1]["name"] = "彭哚菇"
        MapJSONSlice[1]["lv"] = 3
        MapJSONSlice[1]["hp"] = 300
        MapJSONSlice[1]["goods"] = []string{"草莓圣代(HP恢复80)", "铁剑(攻击力+5)", "六点色子"}
    }
    mapJSONSliceRes, err3 := json.Marshal(MapJSONSlice)
    if err3 != nil {
        fmt.Println("map切片集合序列化为json失败...", err3)
        return
    }
    fmt.Println("↓map切片集合序列化为json后:↓")
    fmt.Println(string(mapJSONSliceRes))
    fmt.Println()
复制代码

 

 

 

反序列化

json.Unmarshal([]byte(json字符串), &接收体指针)

 

Json → Struct(结构体)

//People 结构体
type People struct {
    Name string `jsno:"name"`
    Age  int    `json:"age"`
}
复制代码
 1 //将json字符串 反序列化到 struct结构体
 2     jsonStructStr := `{"name":"tom","age":22}`
 3     p := &People{}
 4     err := json.Unmarshal([]byte(jsonStructStr), &p)
 5     if err != nil {
 6         fmt.Println("Unmarshal jsonStr err!", err)
 7         return
 8     }
 9     fmt.Println("↓struct结构体↓")
10     fmt.Println(p)
11     fmt.Println(p.Name)
12     fmt.Println()
复制代码

 

Json → Map(集合,key,value)

复制代码
 1     //将json字符串 反序列化 到map集合
 2     jsonMapStr := `{"goodsName":"华为P40","price":5999,"color":"蓝色"}`
 3     ResMap := map[string]interface{}{}
 4     err2 := json.Unmarshal([]byte(jsonMapStr), &ResMap)
 5     if err2 != nil {
 6         fmt.Println("Unmarshal jsonMap err!", err2)
 7         return
 8     }
 9     fmt.Println("↓map集合↓")
10     fmt.Println(ResMap)
11     fmt.Println(ResMap["goodsName"])
12     fmt.Println()
复制代码

 

 

Json → Map切片(多个map集合,key,value)

复制代码
 1 //将json字符串 反序列化 到map切片集合
 2     jsonMapSliceStr := `[{"goodsName":"华为Mate40","price":4500,"color":"橄榄绿"},{"goodsName":"小米11","price":5999,"color":"青春蓝"}]`
 3     ResMapSlice := make([]map[string]interface{}, 2)
 4 
 5     err3 := json.Unmarshal([]byte(jsonMapSliceStr), &ResMapSlice)
 6     if err3 != nil {
 7         fmt.Println("Unmarshal jsonMapSlice err!", err3)
 8         return
 9     }
10     fmt.Println("↓map切片集合↓")
11     fmt.Println(ResMapSlice)
12     fmt.Println(ResMapSlice[0])
13     fmt.Println()
复制代码

 

posted @   伊凡晴天  阅读(2668)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示