Microsoft日历事件对Go结构的响应

时间:2020-08-07 11:56:57

标签: go microsoft-graph-api

试图将Microsoft的Graph API的响应解组到Go结构中,但是我不断收到错误消息“ json:无法将对象解组到类型为[] struct {DateTime string“ json:” dateTime “”; TimeZone字符串“ json:” timeZone“”}“。

下面是我的结构:

type MicrosoftCalendarEventsResponse struct {   
    Value []struct {
        Etag    string `json:"@odata.etag"`
        Id      string `json:"id"`
        Subject string `json:"subject"`
        Start   []struct {
            DateTime string `json:"dateTime"`
            TimeZone string `json:"timeZone"`
        } `json:"start"`
        End []struct {
            DateTime string `json:"dateTime"`
            TimeZone string `json:"timeZone"`
        } `json:"end"`
        OriginalStartTimeZone string `json:"originalStartTimeZone"`
        OriginalEndTimeZone   string `json:"originalEndTimeZone"`
        ICalUId                    string `json:"iCalUId"`
        ReminderMinutesBeforeStart int    `json:"reminderMinutesBeforeStart"`
        IsReminderOn               bool   `json:"isReminderOn"`
    } `json:"value"`
}

我收到的回复是:

{"@odata.etag":"W/\"8COqS12xxxhwcMA==\"","id":"xxxxx","createdDateTime":"2019-12-05T17:09:41.018502Z","lastModifiedDateTime":"2019-12-05T17:09:41.8919929Z","changeKey":"xxxx","categories":[],"originalStartTimeZone":"W. Europe Standard Time","originalEndTimeZone":"W. Europe Standard Time","iCalUId":"xxx","reminderMinutesBeforeStart":15,"isReminderOn":true,"hasAttachments":false,"subject":"Something","bodyPreview":"","importance":"normal","sensitivity":"normal","isAllDay":false,"isCancelled":false,"isOrganizer":true,"responseRequested":true,"seriesMasterId":null,"showAs":"busy","type":"singleInstance","webLink":"xxx","onlineMeetingUrl":null,"isOnlineMeeting":false,"onlineMeetingProvider":"unknown","allowNewTimeProposals":true,"recurrence":null,"onlineMeeting":null,"responseStatus":{"response":"organizer","time":"0001-01-01T00:00:00Z"},"body":{"contentType":"html","content":""},"start":{"dateTime":"2019-12-17T17:00:00.0000000","timeZone":"UTC"},"end":{"dateTime":"2019-12-17T17:30:00.0000000","timeZone":"UTC"},"location":{"displayName":"","locationType":"default","uniqueIdType":"unknown","address":{},"coordinates":{}},"locations":[],"attendees":[],"organizer":{"emailAddress":{"name":"John Doe","address":"someone@somewhere.com"}}}

在其中您可以清楚地看到出现错误的部分:

"start":{"dateTime":"2019-12-17T17:00:00.0000000","timeZone":"UTC"}

谁能告诉我我做错了什么?尝试了几个小时而没有任何进展,我真的不知道出什么问题了。

Etag,Id,Subject等其他内容也可以正常工作。它只是嵌套的[]结构不起作用。

1 个答案:

答案 0 :(得分:0)

要捕获日历事件(多个事件),有必要将顶层Values是结构的一部分。

但是,在特定的Value中,您的StartEnd字段也被定义为可能不是您想要的结构片段。

尝试一个简单的结构:

Start struct {
        DateTime string `json:"dateTime"`
        TimeZone string `json:"timeZone"`
} `json:"start"`

End struct {
        DateTime string `json:"dateTime"`
        TimeZone string `json:"timeZone"`
} `json:"end"`