我创建了一个 2d 切片并从后端数据库填充它,但是由于 json.Unmarshal 仅接受 []byte 作为第一个参数,我如何将我的 2d 切片类型转换为 []byte。
这是供参考的示例代码,因为我无法共享内部代码:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
//User sjd
type User struct {
EmailList [][]string `json:"emailList"`
}
func listHandler(w http.ResponseWriter, r *http.Request) {
reqBody, _ := ioutil.ReadAll(r.Body)
var user User
json.Unmarshal(reqBody, &user)
el := user.EmailList
keys := make([][]string, 0)
json.Unmarshal([]byte(el), &keys) //this line not working because []byte(el) not possible
fmt.Println(keys)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(el))//this line not working because []byte(el) not possible
}
func main() {
http.HandleFunc("/", listHandler)
http.ListenAndServe(":8080", nil)
}
样品要求:
{
"emailList": [
[
"akki@gmail.com",
"bakki@gmail.com"
],
[
"lakki@gmail.com",
"jakki@gmail.com"
]
]
}
慕容3067478
相关分类