【发布时间】:2020-03-03 04:55:06
【问题描述】:
我从服务收到具有这种格式的 JSON。
{
"result": {
"bn05deh7jsm86gtlg2l0C": [
{
"index_name": "BASE",
"index_value": 4081512,
"timestamp": "2019-11-05T13:20:00Z",
"op_id": "A0000000001"
},
...
],
"bn05deh7jsm86gtlg2lgC": [
{
"index_name": "BASE",
"index_value": 4728633,
"timestamp": "2019-11-05T13:20:00Z",
"op_id": "A0000000001"
},
...
],
...
}
}
我需要将它转换为一个对象数组,例如 []Measure:
type Measure struct {
IndexName string `json:"index_name"`
IndexValue uint32 `json:"index_value"`
Timestamp time.Time `json:"timestamp"`
OperationID string `json:"op_id"`
Guid string `json:"guid"`
}
Guid 的值应为 bn05deh7jsm86gtlg2l0C、bn05deh7jsm86gtlg2lgC 等。
这是我的代码:
url := "https://myurl.com"
req, err := http.NewRequest("GET", url, nil)
if req != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return nil
}
var measures []Measure
err = json.NewDecoder(resp.Body).Decode(&measures)
if err != nil {
log.Println(err)
}
我该怎么做?
【问题讨论】: