Golang复杂json结构体解析

1、示例一

{<br>"id":"8667597b-bcd9-51de31b655cd",
"name":"ali-redis-analyse",
"category":"db",
"category_display":"数据库",
"type":"redis",
"type_display":"Redis",
"attrs":{<br>  "a":"dklasglkaslkd",<br>  "b":"daskjlgasjl;d"<br>},
"date_created":"2022/10/11 11:48:06 +0800",
"date_updated":"2022/10/11 14:09:17 +0800",
"created_by":"istrator",
"comment":"r-0xi2",
"domain":"6dea0b4b-88eb-3d65e5558cab",
"org_id":"00000000-0000-0000",
"org_name":"Default"<br>}

如果josn对象很复杂的话,结构体写起来也是很麻烦的。但有了这个神器之后就不怕写结构体了,效果如下图

https://mholt.github.io/json-to-go/icon-default.png?t=M85Bhttps://mholt.github.io/json-to-go/?utm_source=wechat_session&utm_medium=social&utm_oi=34398162386944

示例代码:

type AutoGenerated struct {
    ID              string `json:"id"`
    Name            string `json:"name"`
    Category        string `json:"category"`
    CategoryDisplay string `json:"category_display"`
    Type            string `json:"type"`
    TypeDisplay     string `json:"type_display"`
    Attrs           struct {
        A string `json:"a"`
        B string `json:"b"`
    } `json:"attrs"`
    DateCreated string `json:"date_created"`
    DateUpdated string `json:"date_updated"`
    CreatedBy   string `json:"created_by"`
    Comment     string `json:"comment"`
    Domain      string `json:"domain"`
    OrgID       string `json:"org_id"`
    OrgName     string `json:"org_name"`
}
 
func main() {
    var data AutoGenerated
    var s = `{"id":"8667597b-bcd9-51de31b655cd","name":"ali-redis-analyse",
        "category":"db","category_display":"数据库","type":"redis","type_display":"Redis","attrs":{"a":"dajkjfka","b":"djaskj"},
        "date_created":"2022/10/11 11:48:06 +0800","date_updated":"2022/10/11 14:09:17 +0800",
        "created_by":"istrator","comment":"r-0x",
        "domain":"6dea0b4b-88eb-3d65e5558cab","org_id":"00000000-0000-0000-0000","org_name":"Default"}`
    err := json.Unmarshal([]byte(s),&data)
    if err != nil{
        fmt.Println(err)
        return
    }
    fmt.Println(data)
    fmt.Println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
    fmt.Println(data.Attrs.A)
}

结果所示:

转载请备注原文链接!!! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值