golang map转json

前端之家收集整理的这篇文章主要介绍了golang map转json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
// map to json

package main



import (
    "encoding/json"
    "fmt"


)


func main() {

    s := []map[string]interface{}{}

    m1 := map[string]interface{}{"name": "John","age": 10}
    m2 := map[string]interface{}{"name": "Alex","age": 12}

    s = append(s,m1,m2)
    s = append(s,m2)

    b,err := json.Marshal(s)
    if err != nil {
        fmt.Println("json.Marshal Failed:",err)
        return
    }


    fmt.Println("b:",string(b))

}

output:

b: [{“age”:10,”name”:”John”},{“age”:12,”name”:”Alex”},”name”:”Alex”}]

猜你在找的Go相关文章