[英]json.Unmarshal not decoding into inner interface{} correctly
我正在為Golang開發一個Telegram Bot API包裝器(我知道已經有一些,但我正在這樣做以便學習)。 我有一個Response結構:
type Response struct {
Ok bool `json:"ok"`
ErrorCode int64 `json:"error_code"`
Description string `json:"description"`
Result interface{} `json:"result"`
}
我無法知道Result
的實際類型:Telegram服務器可以返回很多內容; 我為每個人制作了一個結構,但我不知道哪個將在Result
。 將HTTP響應中的JSON Unmarshal
到Response
結構中時,除Result
之外,所有內容都正確加載。
在我確定Result將是User
(例如)的函數中,我正在執行user := resp.Result.(*User)
,但是我在運行時遇到以下錯誤: panic: interface conversion: interface {} is map[string]interface {}, not *tgbot.User
。 所以, Result
是map[string]interface{}
。 如何將其轉換為*User
?
謝謝你的回答。
使它成為json.RawMessage
並在第二步解組它,當你確定它是什么類型時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.