亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Go Struct解碼博覽會推送通知響應?

Go Struct解碼博覽會推送通知響應?

Go
精慕HU 2022-11-23 19:59:14
我正在做一項服務,向 Expo 后端發送推送通知。一旦 http 調用 Expo 以波紋管格式響應(根據 Expo):{  "data": [    {      "status": "error" | "ok",      "id": string, // this is the Receipt ID      // if status === "error"      "message": string,      "details": JSON    },    ...  ],  // only populated if there was an error with the entire request  "errors": [{    "code": number,    "message": string  }]}這是一個提供的響應示例:{  "data": [    {      "status": "error",      "message": "\\\"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]\\\" is not a registered push notification recipient",      "details": {        "error": "DeviceNotRegistered"      }    },    {      "status": "ok",      "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"    }  ]}我創建了結構來解碼響應。現在我嘗試解碼“詳細信息”字段的響應時出錯,無論我在我的結構中使用什么類型。我如何處理將 expo 標記為“JSON”的字段?import (    uuid "github.com/satori/go.uuid")type PushResult struct {    Errors []ErrorDetails `json:"errors,omitempty"`    Datas   []DataPart     `json:"data,omitempty"`}type ErrorDetails struct {    Code    int32  `json:"code,omitempty"`    Message string `json:"message,omitempty"`}type DataPart struct {    Status  string    `json:"status,omitempty"`    ID      uuid.UUID `json:"id,omitempty"`    Message string    `json:"message,omitempty"`    Details string `json:"details,omitempty"` //also tried map[string]string and interface{}}這是我正在使用的結構。我也通過查看示例進行了嘗試:type DataPart struct {    Status  string    `json:"status,omitempty"`    ID      uuid.UUID `json:"id,omitempty"`    Message string    `json:"message,omitempty"`    Details struct{           Error string `json:"error"`} `json:"details,omitempty"` }但每次都會收到類似“json:無法將對象解組到 Go struct 字段 DataPart.data.details”之類的錯誤
查看完整描述

1 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

我剛剛使用您的響應示例創建了您的結構,它工作得很好。

操場

也就是說,如果您想要一種更好的調試“解組錯誤”的方法,您可以解包它。這樣您就可以打印其他數據。

var t *json.UnmarshalTypeError
if errors.As(err, &t) {
    spew.Dump(t)
}

示例-> 我將錯誤類型更改為整數,這樣我就可以偽造錯誤。

OBS:請注意,您的“數據”是一個數組,只有第一個有錯誤。


查看完整回答
反對 回復 2022-11-23
  • 1 回答
  • 0 關注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號