2 回答

TA貢獻1873條經驗 獲得超9個贊
考慮到兩個感興趣的自定義字段,您可能最終會得到類似的結果,但如果您只需要名稱,則可以進一步縮減結構。
type AutoGenerated struct {
? ? Fields struct {
? ? ? ? Customfield11343 struct {
? ? ? ? ? ? Self? ? ? ? ?string `json:"self"`
? ? ? ? ? ? Name? ? ? ? ?string `json:"name"`
? ? ? ? ? ? Key? ? ? ? ? string `json:"key"`
? ? ? ? ? ? EmailAddress string `json:"emailAddress"`
? ? ? ? ? ? DisplayName? string `json:"displayName"`
? ? ? ? ? ? Active? ? ? ?bool? ?`json:"active"`
? ? ? ? ? ? TimeZone? ? ?string `json:"timeZone"`
? ? ? ? } `json:"customfield_11343"`
? ? ? ? Customfield12046 []struct {
? ? ? ? ? ? Self? string `json:"self"`
? ? ? ? ? ? Value string `json:"value"`
? ? ? ? ? ? ID? ? string `json:"id"`
? ? ? ? } `json:"customfield_12046"`
? ? } `json:"fields"`
}
您得到的效果是,提要中的所有額外信息都被丟棄,但您可以非常干凈地獲得所需的數據。

TA貢獻1780條經驗 獲得超5個贊
這是一個困難的問題,因為第二個是數組形式。這使得使用地圖變得困難。
對于第一個,使用起來很簡單:
type JiraCustomField struct {
? ? Self? ? ? ? ?string `json:"self"`
? ? Name? ? ? ? ?string `json:"name"`
? ? Key? ? ? ? ? string `json:"key"`
? ? EmailAddress string `json:"emailAddress"`
? ? DisplayName? string `json:"displayName"`
? ? Active? ? ? ?bool? ?`json:"active"`
? ? TimeZone? ? ?string `json:"timeZone"`
}
type JiraPayload struct {
? ? Expand string? ? ? ? ? ? ? ? ? ? ?`json:"expand"`
? ? ID? ? ?string? ? ? ? ? ? ? ? ? ? ?`json:"id"`
? ? Key? ? string? ? ? ? ? ? ? ? ? ? ?`json:"key"`
? ? Fields map[string]JiraCustomField `json:"fields"`
}
https://play.golang.org/p/y8-g6r0kInV
具體來說,這部分Fields map[string]JiraCustomField
對于第二種情況,看起來您需要以數組形式(例如Fields map[string][]JiraCustomField
.
在這種情況下,我認為您需要制作自己的解組器。
您可以使用自定義 Unmarshal/marshaler 執行的操作是使用 Reflection 包并檢查它是數組還是結構。如果它是一個結構體,則將其放入數組中,并將其存儲在Fields map[string][]JiraCustomField
.
- 2 回答
- 0 關注
- 171 瀏覽
添加回答
舉報