将字符串响应转换为golang中的map

时间:2019-04-06 21:13:43

标签: go jira custom-fields jira-rest-api jira-rest-java-api

我正在使用https://godoc.org/github.com/andygrunwald/go-jira#IssueService.GetCustomFields获取自定义字段,并且尝试使用一些数据。

func getsomedata(issue_id string) {
    issue, _, _ := jiraClient.Issue.Get(issue_id, nil)
    fields, _, _ := jiraClient.Issue.GetCustomFields(issue_id)
    data := fields["customfield_123456"]
}

类似于以下内容的内容(未格式化)作为单个字符串返回,如何将其转换回结构或映射?最终目标是存储“ key.value”和“ name.value”

[
  map[
    key:key.value
    name:name.value
  ]
  map[
    key:key.value
    name:name.value
  ]
]

1 个答案:

答案 0 :(得分:0)

我想通了,类型断言是解决此问题的方法。

func getsomedata(issue_id string) {
    issue, _, _ := jiraClient.Issue.Get(issue_id, nil)

    // This returns as a slice of interfaces []interfaces{}
    data := issue.Fields.Unknowns["customfield_12345"]

    // Will use the length of the slice in the for loop below
    s := reflect.ValueOf(data)

    // For each index index in the slice, do...
    for i := 0; i < s.Len(); i++ {
        // Use type assertion to get the value I need for all indexes "i"
        d := data.([]interface{})[i].(map[string]interface{})
        fmt.Printf("%v\n", d["key"].(string))
    }
}