1 回答
TA贡献1773条经验 获得超3个赞
UnmarshalJSON[]byte作为user. Marshal user.EmailList以 JSON 作为[]byte. 例如,
package main
import (
"encoding/json"
"fmt"
"os"
)
type User struct {
EmailList [][]string `json:"emailList"`
}
func main() {
// rBody, err := ioutil.ReadAll(r.Body)
rBody := []byte(`
{
"emailList": [
[
"akki@gmail.com",
"bakki@gmail.com"
],
[
"lakki@gmail.com",
"jakki@gmail.com"
]
]
}
`)
fmt.Println(string(rBody))
var user User
err := json.Unmarshal(rBody, &user)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Println(user.EmailList)
wData, err := json.Marshal(user.EmailList) // wData is []byte
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Println(wData) // wData is []byte
fmt.Println(string(wData))
// _, err = w.Write(wData)
}
游乐场:https://play.golang.org/p/S3HWiTqz--B
输出:
{
"emailList": [
[
"akki@gmail.com",
"bakki@gmail.com"
],
[
"lakki@gmail.com",
"jakki@gmail.com"
]
]
}
[[akki@gmail.com bakki@gmail.com] [lakki@gmail.com jakki@gmail.com]]
[91 91 34 97 107 107 105 64 103 109 97 105 108 46 99 111 109 34 44 34 98 97 107 107 105 64 103 109 97 105 108 46 99 111 109 34 93 44 91 34 108 97 107 107 105 64 103 109 97 105 108 46 99 111 109 34 44 34 106 97 107 107 105 64 103 109 97 105 108 46 99 111 109 34 93 93]
[["akki@gmail.com","bakki@gmail.com"],["lakki@gmail.com","jakki@gmail.com"]]
- 1 回答
- 0 关注
- 88 浏览
添加回答
举报