登录
首页 >  Golang >  Go问答

有没有更简便的方式在 Go 中解析这个 json 数据?

来源:stackoverflow

时间:2024-02-24 21:27:27 419浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《有没有更简便的方式在 Go 中解析这个 json 数据?》,聊聊,我们一起来看看吧!

问题内容

我正在尝试将 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"]

获得他们所属的团队。

获取他们所在的团队有点粗糙,除了必须输入断言、设置索引,然后再次输入断言之外,是否有更干净的方法来获取团队?

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

   }

谢谢


解决方案


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

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

您得到的效果是,提要中的所有额外信息都被丢弃,但您可以非常干净地获得所需的数据。

这是一个困难的问题,因为第二个是数组形式。这使得使用地图变得困难。

对于第一个,使用起来很简单:

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

在这种情况下,我认为您需要制作自己的解组器。这是一个很好的教程:https://blog.gopheracademy.com/advent-2016/advanced-encoding-decoding/

您可以使用自定义 unmarshal/marshaler 执行的操作是使用 reflection 包并检查它是数组还是结构。如果它是一个结构体,则将其放入数组中,并将其存储在 fields map[string][]jiracustomfield 中。

到这里,我们也就讲完了《有没有更简便的方式在 Go 中解析这个 json 数据?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>