I have a struct for image data
type ImageData struct {
Name string
Data []byte
}
The Data field is an image converted to bytes.
I have jsonImages
like [{"Data":<many-many bytes>, "Name":"abracadabra"}]
and var imagesData []ImageData
. And when I try to use json.Unmarshal([]byte(jsonImages), &imagesData)
as a result of fmt.Println(imagesData)
I receive [{abracadabra []}]
. The Data field is empty. What am I doing wrong? Thanks for any help!
I tried replicating the scenario with below snippet It worked fine.Is this the one you are trying:
package main
import (
"encoding/json"
"fmt"
)
type Imgdta struct {
Name string
Dta []byte
}
func main() {
var Imgdta1 Imgdta
var Imgdta2 Imgdta
Imgdta1.Dta = []byte("asfafalsffa")
Imgdta1.Name = "asnakakad"
imgjson, _ := json.Marshal(Imgdta1)
fmt.Println("Input Json:-", string(imgjson))
json.Unmarshal((imgjson), &Imgdta2)
fmt.Println((Imgdta2))
}
... Result:
Input Json:- {"Name":"asnakakad","Dta":"YXNmYWZhbHNmZmE="}
{asnakakad [97 115 102 97 102 97 108 115 102 102 97]}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.