我在 URL 中收到以下响应,我想解组它,但我无法这样做。这是我想要解组的那种响应。
[
{"title": "Angels And Demons", "author":"Dan Brown", "tags":[{"tagtitle":"Demigod", "tagURl": "/angelDemon}] }
{"title": "The Kite Runner", "author":"Khalid Hosseinei", "tags":[{"tagtitle":"Kite", "tagURl": "/kiteRunner"}] }
{"title": "Dance of the dragons", "author":"RR Martin", "tags":[{"tagtitle":"IronThrone", "tagURl": "/got"}] }
]
我正在尝试解组这种响应,但无法这样做。这是我正在尝试编写的代码。
res, err := http.Get(url)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Couldn't get the html response")
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Couldn't read the response")
}
s := string(b)
var data struct {
Content []struct {
Title string `json:"title"`
Author string `json:"author"`
Tags map[string]string `json:"tags"`
}
}
if err := json.Unmarshal([]byte(s), &data); err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Un-marshalling could not be done.")
}
fmt.Println(data.Content)
任何人都可以在这方面帮助我吗?提前致谢。
慕婉清6462132
蝴蝶刀刀
慕容708150
相关分类