I have created a 2d slices and populated it from backend database but as json.Unmarshal accepts only []byte as first parameter how can I typecast my 2d slice to []byte.
Here is the sample code for reference as I can't share internal code:
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)
}
Sample request:
{
"emailList": [
[
"akki@gmail.com",
"bakki@gmail.com"
],
[
"lakki@gmail.com",
"jakki@gmail.com"
]
]
}