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对象很复杂的话,结构体写起来也是很麻烦的。但有了这个神器之后就不怕写结构体了,效果如下图
示例代码:
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)
}
结果所示:
转载请备注原文链接!!!