抱歉,我是golang的菜鸟。我试图通过调用其api从松弛状态获取提醒列表。我需要从响应中提取时间戳和用户ID。但是在解组json时,我无法将值复制到struct。
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
type ReadingHttpResponse struct {
ID string `json:"id"`
Creator string `json:"creator"`
User string `json:"user"`
Text string `json:"text"`
Recurring bool `json:"recurring"`
Time time.Time `json:"time"`
CompleteTS int `json:"complete_ts"`
}
func main() {
url := "https://slack.com/api/reminders.list"
var bearer = "Bearer " + "My Bearer token"
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", bearer)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
var m ReadingHttpResponse
err = json.Unmarshal(body, &m)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%#v\r\n", m)
}
“ ok”:true,“提醒”:[{“ id”:“ bacsasdad”,“ creator”:“ asdasdww”,“ user”:“ asdasdad”,“ text”:“您好,这仅用于测试目的“,” recurring“:false,” time“:1578470823,” complete_ts“:0}]}}
main.ReadingHttpResponse {ID:“”,创建者:“”,用户:“”,文本:“”,重复:false,Time:time.Time {wall:0x0,ext:0,loc:(* time。位置)(无)},完整的TS:0}
您的结构与JSON响应不符,应该像:
type ReadingHttpResponse struct {
Ok bool `json:"ok"`
Reminders []Reminder `json:"reminders"`
}
type Reminder struct {
ID string `json:"id"`
Creator string `json:"creator"`
User string `json:"user"`
Text string `json:"text"`
Recurring bool `json:"recurring"`
Time int `json:"time"`
CompleteTs int `json:"complete_ts"`
}