4.slice转json
最后更新于:2022-04-02 04:45:32
slice转json
~~~
package main
import (
"encoding/json"
"fmt"
)
func testSlice() {
var m map[string]interface{}
var s []map[string]interface{}
m = make(map[string]interface{})
m["username"] = "user1"
m["age"] = 18
m["sex"] = "man"
s = append(s, m)
m = make(map[string]interface{})
m["username"] = "user2"
m["age"] = 29
m["sex"] = "female"
s = append(s, m)
data, err := json.Marshal(s)
if err != nil {
fmt.Printf("json.marshal failed, err:", err)
return
}
fmt.Printf("%s\n", string(data))
}
func main() {
testSlice()
fmt.Println("--------")
}
~~~
输出结果:
~~~
[{"age":18,"sex":"man","username":"user1"},{"age":29,"sex":"female","username":"user2"}]
--------
~~~
';