有没有更简单的方法可以在Go中解码此json? [英] Is there a simpler way to decode this json in Go?

查看:99
本文介绍了有没有更简单的方法可以在Go中解码此json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Jira的一些JSON解析为变量.这是使用go-jira软件包( https://godoc.org/github.com/andygrunwald/go-jira )

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"].(地图[string] interface {})["name"]

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

获得他们所属的团队.

让他们所在的团队有点麻烦,除了必须输入assert,设置索引,然后再次输入assert之外,还有没有一种更干净的方法来使团队得到支持?

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?

这是完整的json(已修改,但结构相同,但时间太长了):

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

   }

谢谢

推荐答案

我解决此类问题的方法是:

The way I go about problems like this is:

  1. 复制一些我感兴趣的JSON并将其粘贴到 https://mholt.github.io/json-to-go/
  2. 删除不感兴趣的字段.
  3. 只需读取数据并解组即可.

给定两个感兴趣的自定义字段,您可能最终会得到类似的结果,但是如果您只需要名称,则可以进一步简化结构.

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

您得到的结果是,Feed中的所有其他信息都被丢弃,但是您获得的数据非常干净.

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

这篇关于有没有更简单的方法可以在Go中解码此json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆