我正在嘗試將 JSON 響應模板化到前端,我當前的結構是這樣的: type Response struct { WhoisRecord struct { CreatedDate time.Time `json:"createdDate"` UpdatedDate time.Time `json:"updatedDate"` ExpiresDate time.Time `json:"expiresDate"` Registrant struct { Name string `json:"name"` Organization string `json:"organization"` Street1 string `json:"street1"` City string `json:"city"` State string `json:"state"` Country string `json:"country"` CountryCode string `json:"countryCode"` Email string `json:"email"` Telephone string `json:"telephone"` Fax string `json:"fax"` } `json:"registrant"` AdministrativeContact struct { Name string `json:"name"` Organization string `json:"organization"` Street1 string `json:"street1"` City string `json:"city"` State string `json:"state"` Country string `json:"country"` CountryCode string `json:"countryCode"` Email string `json:"email"` Telephone string `json:"telephone"` Fax string `json:"fax"` } `json:"administrativeContact"` TechnicalContact struct { Name string `json:"name"` Organization string `json:"organization"` Street1 string `json:"street1"` City string `json:"city"` State string `json:"state"`}然后我解組這個 json 響應,并將它傳遞到前端(我正在使用 GIN)(響應被重新聲明為res)c.HTML(200,"homework.html", gin.H{ "whois": res, })但這是我遇到問題的地方,代碼可以工作,但我不確定如何模板化它,因為它是嵌套的。例如,我想在單獨的表格中顯示注冊人、管理人員和技術聯系人的詳細信息(返回的所有字段)。以及顯示創建、更新和過期的日期。然后通過顯示注冊商、ips 和名稱服務器(在本例中為 下的hostnames字段NameServers)來完成但是這些都不起作用(我如何在一個字段中顯示注冊人姓名,然后在另一個字段中顯示技術名稱?我顯然把模板搞砸了,我對它的理解有點歪曲)。我已經閱讀了我所能閱讀的所有內容,我試圖劃分結構并作為單獨的結構,等等。沒有任何工作。有人可以指出我正確的方向并舉例嗎?謝謝!
1 回答

海綿寶寶撒
TA貢獻1809條經驗 獲得超8個贊
模板字段相對于當前上下文 {{.}} 進行評估。以下使用包含“whois”作為鍵和res
值的映射來評估模板:
in.H{ "whois": res})
的值{{.whois}}
是您的結構,您可以從那里訪問結構字段。所以你可以這樣做:
{{.whois.Registrant.Name}}
- 1 回答
- 0 關注
- 149 瀏覽
添加回答
舉報
0/150
提交
取消