簡體   English   中英

有沒有更簡單的方法可以在Go中解碼此json?

[英]Is there a simpler way to decode this json in Go?

我正在嘗試從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"]

讓他們成為團隊的一部分。

讓他們加入的團隊有點毛骨悚然,除了必須輸入assert,設置索引,然后再次輸入assert之外,還有一種更干凈的方法來使團隊嗎?

這是完整的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":"flast@redacted.com",
         "displayName":"first last",
         "active":true,
         "timeZone":"Europe/London"
      },
      "customfield_12046":[  
         {  
            "self":"https://jira.redacted.com/rest/api/2/customFieldOption/12045",
            "value":"diy",
            "id":"12045"
         }
      ],

   }

謝謝

這是一個艱難的過程,因為第二個是數組形式。 這使得使用地圖變得困難。

對於第一個,使用起來很簡單:

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這樣的數組形式。

在這種情況下,我認為您需要制作自己的Unmarshaler。 這是一個很好的教程: https : //blog.gopheracademy.com/advent-2016/advanced-encoding-decoding/

您可以使用自定義的Unmarshal / marshaler進行處理,就是使用Reflection包並檢查它是數組還是結構。 如果是結構,則將其放入數組中,並將其存儲在Fields map[string][]JiraCustomField

我解決此類問題的方法是:

  1. 復制一些我感興趣的JSON並將其粘貼到https://mholt.github.io/json-to-go/
  2. 刪除不需要的字段。
  3. 只需讀取數據並解組即可。

給定兩個感興趣的自定義字段,最終可能會得到類似的結果,但是如果您只需要名稱,則可以進一步簡化結構。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM