我在反序列化我的對象時遇到問題。我使用該對象的接口來調(diào)用序列化,并且通過讀取輸出,序列化工作完美。這是我的對象的底層結(jié)構(gòu):type pimp struct { Price int ExpDate int64 BidItem Item CurrentBid int PrevBidders []string}這是它實現(xiàn)的接口:type Pimp interface { GetStartingPrice() int GetTimeLeft() int64 GetItem() Item GetCurrentBid() int SetCurrentBid(int) GetPrevBidders() []string AddBidder(string) error Serialize() ([]byte, error)}Serialize() 方法:func (p *pimp) Serialize() ([]byte, error) { return json.Marshal(*p)}您可能已經(jīng)注意到,pimp 有一個名為 Item 的變量。這個項目也是一個接口:type item struct { Name string}type Item interface { GetName() string}現(xiàn)在序列化此類對象的樣本會返回以下 JSON:{"Price":100,"ExpDate":1472571329,"BidItem":{"Name":"ExampleItem"},"CurrentBid":100,"PrevBidders":[]}這是我的反序列化代碼:func PimpFromJSON(content []byte) (Pimp, error) { p := new(pimp) err := json.Unmarshal(content, p) return p, err}但是,運行它會給我以下錯誤:json: cannot unmarshal object into Go value of type Auction.Item任何幫助表示贊賞。
json:無法將對象解組為 Auction.Item 類型的 Go 值
慕無忌1623718
2022-03-07 16:02:25