When I try to marshal [] bytes in JSON format, I only get a strange string.
Check out the following code.
I have two doubts:
How can I output byte [] in JSON?
Why does [] byte become this string?
package main import ( "encoding/json" "fmt" "os" ) func main() { type ColorGroup struct { ByteSlice []byte SingleByte byte IntSlice []int } group := ColorGroup{ ByteSlice: []byte{0,0,0,1,2,3}, SingleByte: 10, IntSlice: []int{0,0,0,1,2,3}, } b, err := json.Marshal(group) if err != nil { fmt.Println("error:", err) } os.Stdout.Write(b) }
output:
{"ByteSlice":"AAAAAQID","SingleByte":10,"IntSlice":[0,0,0,1,2,3]}
golang playground: https://play.golang.org/p/wanppBGzNR
source share