我正在做一項(xiàng)服務(wù),向 Expo 后端發(fā)送推送通知。一旦 http 調(diào)用 Expo 以波紋管格式響應(yīng)(根據(jù) 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 }]}這是一個(gè)提供的響應(yīng)示例:{ "data": [ { "status": "error", "message": "\\\"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]\\\" is not a registered push notification recipient", "details": { "error": "DeviceNotRegistered" } }, { "status": "ok", "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" } ]}我創(chuàng)建了結(jié)構(gòu)來解碼響應(yīng)?,F(xiàn)在我嘗試解碼“詳細(xì)信息”字段的響應(yīng)時(shí)出錯(cuò),無論我在我的結(jié)構(gòu)中使用什么類型。我如何處理將 expo 標(biāo)記為“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{}}這是我正在使用的結(jié)構(gòu)。我也通過查看示例進(jìn)行了嘗試: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"` }但每次都會(huì)收到類似“json:無法將對(duì)象解組到 Go struct 字段 DataPart.data.details”之類的錯(cuò)誤
1 回答

繁華開滿天機(jī)
TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
我剛剛使用您的響應(yīng)示例創(chuàng)建了您的結(jié)構(gòu),它工作得很好。
也就是說,如果您想要一種更好的調(diào)試“解組錯(cuò)誤”的方法,您可以解包它。這樣您就可以打印其他數(shù)據(jù)。
var t *json.UnmarshalTypeError if errors.As(err, &t) { spew.Dump(t) }
示例-> 我將錯(cuò)誤類型更改為整數(shù),這樣我就可以偽造錯(cuò)誤。
OBS:請(qǐng)注意,您的“數(shù)據(jù)”是一個(gè)數(shù)組,只有第一個(gè)有錯(cuò)誤。
- 1 回答
- 0 關(guān)注
- 86 瀏覽
添加回答
舉報(bào)
0/150
提交
取消