duan19750503 2016-04-02 06:21
浏览 37
已采纳

是否有使用json编码数组/映射的函数?

Is there a function in golang to encode an array/map using json? Something similar to PHP's json_encode() function is what I'm looking for.

  • 写回答

1条回答 默认 最新

  • dtrj21373 2016-04-02 06:30
    关注

    you can encode data structures to json in golang using the encoding/json package like this

    package main
    
    import (
        "encoding/json"
        "fmt"
        "os"
    )
    
    func main() {
        group := map[string]interface{} {
            "name": "John Doe",
            "age": 112,
        }
        b, err := json.Marshal(group) // this converts the structure into json
        if err != nil {
            fmt.Println("error:", err)
        }
        os.Stdout.Write(b)
    }
    

    golang json package

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?