我正在嘗試動態創建一個結構片段,我可以將數據編組到其中,但在運行時基于一些未知的結構類型來執行它。我這樣做是因為我想要一個通用的代碼,可以解組任何實現特定接口的東西。例如(偽代碼)func unmarshalMyData(myInterface MyInterface, jsonData []byte) { targetSlice := myInterface.EmptySlice() json.Unmarshal(jsonData, &targetSlice)}我嘗試了幾種不同的選擇。似乎最有希望的是使用reflect包來構造切片。不幸的是,在解組之后,動態創建的切片的類型為[]interface{}. 如果我在解組之前打印出動態創建的切片的類型,它將打印[]*main.myObj. 誰能解釋為什么?查看游樂場鏈接: https: //play.golang.org/p/vvf1leuQeY是否有其他方法可以動態創建正確解組的結構切片?我知道json.RawMessage,但有幾個原因我不能使用它......我的 json 中沒有“類型”字段。此外,我正在解組的代碼沒有關于它正在解組的結構的編譯時知識。它只知道結構實現了特定的接口。一些讓我困惑的代碼為什么動態創建的切片在解組后不保持其類型:package mainimport ( "encoding/json" "fmt" "reflect")func main() { input := `[{"myField":"one"},{"myField":"two"}]` myObjSlice := []*myObj{} fmt.Printf("Type of myObjSlice before unmarshalling %T\n", myObjSlice) err := json.Unmarshal([]byte(input), &myObjSlice) if err != nil { panic(err) } fmt.Printf("Type of myObjSlice after unmarshalling %T\n", myObjSlice) myConstructedObjSlice := reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(&myObj{})), 0, 0).Interface() fmt.Printf("Type of myConstructedObjSlice before unmarshalling %T\n", myConstructedObjSlice) err = json.Unmarshal([]byte(input), &myConstructedObjSlice) if err != nil { panic(err) } fmt.Printf("Type of myConstructedObjSlice after unmarshalling %T\n", myConstructedObjSlice)}type myObj struct { myField string}輸出:Type of myObjSlice before unmarshalling []*main.myObjType of myObjSlice after unmarshalling []*main.myObjType of myConstructedObjSlice before unmarshalling []*main.myObjType of myConstructedObjSlice after unmarshalling []interface {}
1 回答

慕桂英3389331
TA貢獻2036條經驗 獲得超8個贊
您正在構建一個[]*myObjusing reflect,但隨后您將傳遞一個*interface{}to json.Unmarshal。json 包將指針的目標視為 type interface{},因此使用其默認類型來解組。您需要創建一個指向您創建的切片類型的指針,因此對該Interface()方法的調用會返回您想要解組的確切類型,即*[]*myObj.
sliceType := reflect.SliceOf(reflect.TypeOf(&myObj{}))
slicePtr := reflect.New(sliceType)
err = json.Unmarshal([]byte(input), slicePtr.Interface())
- 1 回答
- 0 關注
- 138 瀏覽
添加回答
舉報
0/150
提交
取消