doupingyun73833 2019-08-05 21:14
浏览 78
已采纳

有没有更简单的方法可以在Go中解码此json?

I am trying to parse some JSON from Jira to variables. This is using the go-jira package (https://godoc.org/github.com/andygrunwald/go-jira)

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):

{    
 "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"
         }
      ],

   }

Thanks

  • 写回答

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 https://mholt.github.io/json-to-go/
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败