登录
首页 >  Golang >  Go问答

响应 Microsoft 日历事件的 Go 结构

来源:stackoverflow

时间:2024-02-23 21:51:26 250浏览 收藏

哈喽!今天心血来潮给大家带来了《响应 Microsoft 日历事件的 Go 结构》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

一直在尝试将 microsoft graph api 的响应解组到 go 结构中,但我不断收到错误“json: 无法将对象解组到 go 结构字段 .value.start of type []struct { datetime string "json:"datetime ""; 时区字符串 "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":"[email protected]"}}}

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

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

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

etag、id、subject 等其他内容均工作正常。只有嵌套的 [] 结构不起作用。


解决方案


为了捕获日历事件(复数),顶层 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"`

今天关于《响应 Microsoft 日历事件的 Go 结构》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>