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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题