有没有更简单的方法在 Go 中解码这个 json?
Is there a simpler way to decode this json in Go?
我正在尝试将一些 JSON 从 Jira 解析为变量。这是使用 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":"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"
}
],
}
谢谢
这很难,因为第二个是数组形式。这使得很难使用地图。
第一个,用起来很简单:
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
这样的数组形式。
在这种情况下,我认为您需要制作自己的 Unmarshaler。这是一个很好的教程:https://blog.gopheracademy.com/advent-2016/advanced-encoding-decoding/
您可以对自定义 Unmarshal/marshaler 做的是使用 Reflection 包并检查它是数组还是结构。如果它是一个结构,那么将它放入一个数组中,并将它存储在 Fields map[string][]JiraCustomField
中。
我解决此类问题的方法是:
- 复制一些JSON我感兴趣的内容并粘贴到https://mholt.github.io/json-to-go/
- 删除不感兴趣的字段。
- 只需读取数据并解组。
考虑到两个感兴趣的自定义字段,您最终可能会得到类似这样的结果,但如果您只需要名称,则可以进一步缩减结构。
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"`
}
你得到的效果是提要中的所有额外信息都被丢弃了,但是你得到了你想要的数据非常干净。
我正在尝试将一些 JSON 从 Jira 解析为变量。这是使用 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":"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"
}
],
}
谢谢
这很难,因为第二个是数组形式。这使得很难使用地图。
第一个,用起来很简单:
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
这样的数组形式。
在这种情况下,我认为您需要制作自己的 Unmarshaler。这是一个很好的教程:https://blog.gopheracademy.com/advent-2016/advanced-encoding-decoding/
您可以对自定义 Unmarshal/marshaler 做的是使用 Reflection 包并检查它是数组还是结构。如果它是一个结构,那么将它放入一个数组中,并将它存储在 Fields map[string][]JiraCustomField
中。
我解决此类问题的方法是:
- 复制一些JSON我感兴趣的内容并粘贴到https://mholt.github.io/json-to-go/
- 删除不感兴趣的字段。
- 只需读取数据并解组。
考虑到两个感兴趣的自定义字段,您最终可能会得到类似这样的结果,但如果您只需要名称,则可以进一步缩减结构。
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"`
}
你得到的效果是提要中的所有额外信息都被丢弃了,但是你得到了你想要的数据非常干净。