响应:
{
“元”:{
“query_time”:0.039130201,
“分页”:{
“偏移”:1345,
“限制”:5000,
总:1345
},
结构:
输入InLicense struct {
总共int16 json:"total,omitempty"
}
输入OutLicense struct {
分页[] InLicense json:"pagination,omitempty"
}
输入MetaLicense struct {
元[] OutLicense json:"meta,omitempty"
}
函数内部的代码片段:
req,err:= http.NewRequest(“GET”,,nil)
如果err!= nil {
//处理错误
}
客户:=&http.Client {}
resp,err:= client.Do(要求)
如果err!= nil {
log.Println(“Error:”,err)
}
延迟resp.Body.Close()
val:=&MetaLicense {}
err = json.NewDecoder(resp.Body).Decode(&val)
if err != nil {
log.Fatal(err)
}
for _, s := range val.Meta {
for _, a := range s.Pagination {
fmt.Println(a.Total)
}
}
}运行此代码后,出现以下错误:
json:无法将对象解码为[] OutLicense类型的Go结构字段MetaLicense.meta
为了正确解码[] OutLicense,需要使用哪种类型?我无法以其他方式打印它,但是它使用{}和Strings.Trim打印。
最佳答案
您应该只使用一个具有实际类型的简单字段声明,而不要使用以下类型的[]
:
type InLicense struct {
Total int16 json:"total,omitempty"
}
type OutLicense struct {
Pagination InLicense json:"pagination,omitempty"
}
type MetaLicense struct {
Meta OutLicense json:"meta,omitempty"
}
关于go - 尝试从JSON响应中获取 “Total”的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63163215/