duanjianshen4871
duanjianshen4871
采纳率100%
2015-03-20 11:57 阅读 71
已采纳

Golang将哈希表转换为JSON

How to convert a Hashtable into JSON in GoLang ? I want to convert a hash table into a JSON to be able to send it as POST request payload

data := make(map[string]string)
data["a"] = "b"
data["c"] = "d"

How do i convert this to JSON ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongyang448966 dongyang448966 2015-03-20 12:23

    Like that

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main() {
        data := make(map[string]string)
        data["a"] = "b"
        data["c"] = "d"
        bytes, err := json.Marshal(data)
        if err != nil {
            fmt.Println(err)
            return
        }
        text := string(bytes)
        fmt.Println(text)
    }
    

    Playground

    点赞 评论 复制链接分享

相关推荐