ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
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"}] -------- ~~~