2019-08-05 21:14
浏览 78


I am trying to parse some JSON from Jira to variables. This is using the go-jira package (

Currently I have some code to get the developer:

dev := jiraIssue.Fields.Unknowns["customfield_11343"].(map[string]interface{})["name"]

and team := jiraIssue.Fields.Unknowns["customfield_12046"].([]interface{})[0].(map[string]interface{})["value"]

to get the team they are a part of from.

Getting the team they are on is a bit gross, is there a cleaner way to get the team besides having to type assert, set the index, then type assert again?

Here is the complete json (modified but structure is same, its way too long):

         "displayName":"first last",



图片转代码服务由CSDN问答提供 功能建议

我正在尝试将Jira的一些JSON解析为变量。 这是使用go-jira软件包( / andygrunwald / go-jira


dev:= jiraIssue .Fields.Unknowns [“ customfield_11343”]。(地图[string] interface {})[“ name”]

team:= jiraIssue.Fields。 Unknowns [“ customfield_12046”]。([[] interface {})[0]。(map [string] interface {})[“ value”]


让他们加入的团队有点毛骨悚然,除了必须输入断言,设置索引然后 再输入一次断言?


“ expand”:  “ renderedFields,名称,模式,操作,editmeta,changelog,versionedRepresentations”,
“ id”:“ 136944”,
“ self”:“  / 136944“,
” key“:” RM-2506“,
” fields“:{
” customfield_11343“:{
” self“:”  / user?username = flast“,
” name“:” flast“,
” key“:” flast“,
” emailAddress“:”“,
” displayName“:” first  last“,
” active“:true,
” timeZone“:”欧洲/伦敦“ 
” customfield_12046“:[
” self“:” https://jira.redacted  .com / rest / api / 2 / customFieldOption / 12045“,
” value“:” diy“,
” id“:” 12045“ 


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doujiyun0041 2019-08-05 23:12

    The way I go about problems like this is:

    1. Copy some JSON with things I am interested in and paste it into
    2. Remove fields that aren´t of interest.
    3. Just read the data and unmarshal.

    You might end up with something like this given the two custom fields of interest, but you can cut the structure down further if you just need the name.

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

    The effect you get is that all extra information in the feed is discarded, but you get the data you want very cleanly.

    打赏 评论
  • dongpangzan6425 2019-08-05 23:01

    This is a tough one since the second one is in an array form. It makes it hard to use a map.

    For the first one, it's simple enough to use:

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

    Specifically this part Fields map[string]JiraCustomField for the second case it looks like you need it in an array form like Fields map[string][]JiraCustomField.

    In a case like this, I think you'll need to make your own Unmarshaler. This is a good tutorial:

    What you could do with your custom Unmarshal/marshaler, is use the Reflection package and check if it's an array or a struct. If it's a struct then put it into an array, and store it in Fields map[string][]JiraCustomField.

    打赏 评论

相关推荐 更多相似问题