dtn36013 2019-04-06 21:13
浏览 164

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

I'm using https://godoc.org/github.com/andygrunwald/go-jira#IssueService.GetCustomFields to get a custom field and I'm attempting to consume some of the data.

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

Something similar to the following (unformatted) is returned as a single string, how can I convert this back into a struct or map? The end goal is to store "key.value" and "name.value"

[
  map[
    key:key.value
    name:name.value
  ]
  map[
    key:key.value
    name:name.value
  ]
]
  • 写回答

1条回答 默认 最新

  • dongshan8953 2019-04-08 00:00
    关注

    I figured it out, type assertion is the way to solve this.

    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
    ", d["key"].(string))
        }
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入