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

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

在 Go 中是否有更簡單的方法來解碼這個 json?

在 Go 中是否有更簡單的方法來解碼這個 json?

Go
aluckdog 2023-07-10 15:02:34
我正在嘗試將 Jira 中的一些 JSON 解析為變量。這是使用 go-jira 包(https://godoc.org/github.com/andygrunwald/go-jira)目前我有一些代碼可以讓開發人員:dev := jiraIssue.Fields.Unknowns["customfield_11343"].(map[string]interface{})["name"]和team := jiraIssue.Fields.Unknowns["customfield_12046"].([]interface{})[0].(map[string]interface{})["value"]獲得他們所屬的團隊。獲取他們所在的團隊有點粗糙,除了必須鍵入斷言、設置索引,然后再次鍵入斷言之外,是否有更干凈的方法來獲取團隊?這是完整的 json(已修改但結構相同,太長了):{     "expand":"renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",   "id":"136944",   "self":"https://jira.redacted.com/rest/api/2/issue/136944",   "key":"RM-2506",   "fields":{        "customfield_11343":{           "self":"https://redacted.com/rest/api/2/user?username=flast",         "name":"flast",         "key":"flast",         "emailAddress":"[email protected]",         "displayName":"first last",         "active":true,         "timeZone":"Europe/London"      },      "customfield_12046":[           {              "self":"https://jira.redacted.com/rest/api/2/customFieldOption/12045",            "value":"diy",            "id":"12045"         }      ],   }
查看完整描述

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"`

}

您得到的效果是,提要中的所有額外信息都被丟棄,但您可以非常干凈地獲得所需的數據。


查看完整回答
反對 回復 2023-07-10
?
翻閱古今

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.


查看完整回答
反對 回復 2023-07-10
  • 2 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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