亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Unmarshal 將內部對象值設置為 json 字符串

Unmarshal 將內部對象值設置為 json 字符串

Go
元芳怎么了 2022-09-05 17:31:24
我正在從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}}


查看完整回答
反對 回復 2022-09-05
  • 1 回答
  • 0 關注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號