我正在從firebase讀取數據,響應為“map[string]interface{}”,例如:Response: { Id: 1, Name: "Marwan", Career: { employeer: "mycompany", salary: "100", }}我有一個結構:type Employee struct { Id int Name string Career CareerType}type CareerType struct { Employeer string Salary string}當我執行以下操作時:marshal, _ := json.Marshal(data)json.Unmarshal(marshal, Emplyee{})結果將為:Reposnse: { Id: 1, Name: "Marwan", Career: "{\"employeer\":\"mycompany\", \"salary\":\"100\"}"}有沒有人知道為什么內在對象(在這種情況下是職業)沒有被解構到一個對象上?難道不應該有智慧的行動來隱含地做到這一點嗎?
1 回答
浮云間
TA貢獻1829條經驗 獲得超4個贊
封送處理數據時,只需傳入與結構對應的元素。例如:
bytes, _ := json.Marshal(data["Response"])
之后,取消編組應按預期工作:
var employee Employee json.Unmarshal(bytes, &employee)
employee現在應如下所示:
{Id:1 Name:Marwan Career:{Employer:mycompany Salary:100}}- 1 回答
- 0 關注
- 90 瀏覽
添加回答
舉報
0/150
提交
取消
