有没有更简单的方法可以在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
我正在尝试将Jira的一些JSON解析为变量。 这是使用go-jira软件包( https://godoc.org/github.com / andygrunwald / go-jira ) p>
当前,我有一些代码可供开发人员使用: p>
和 他们所属的团队。 p>
让他们加入的团队有点毛骨悚然,除了必须输入断言,设置索引然后 再输入一次断言? p>
这是完整的json(已修改,但结构相同,但时间太长了): p>
谢谢 p>
div> dev:= jiraIssue .Fields.Unknowns [“ customfield_11343”]。(地图[string] interface {})[“ name”] code> p>
team:= jiraIssue.Fields。 Unknowns [“ customfield_12046”]。([[] interface {})[0]。(map [string] interface {})[“ value”] code> p>
{
“ expand”: “ renderedFields,名称,模式,操作,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“:”欧洲/伦敦“
},
” customfield_12046“:[
{
” self“:” https://jira.redacted .com / rest / api / 2 / customFieldOption / 12045“,
” value“:” diy“,
” id“:” 12045“
}
],
}
code > pre>
The way I go about problems like this is:
- Copy some JSON with things I am interested in and paste it into https://mholt.github.io/json-to-go/
- Remove fields that aren´t of interest.
- 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.
This is a tough one since the second one is in an array form. It makes it hard to use a map.
For the first one, it's simple enough to use:
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
Specifically this part Fields map[string]JiraCustomField
for the second case it looks like you need it in an array form like Fields map[string][]JiraCustomField
.
In a case like this, I think you'll need to make your own Unmarshaler. This is a good tutorial: https://blog.gopheracademy.com/advent-2016/advanced-encoding-decoding/
What you could do with your custom Unmarshal/marshaler, is use the Reflection package and check if it's an array or a struct. If it's a struct then put it into an array, and store it in Fields map[string][]JiraCustomField
.