简体   繁体   中英

json.unmarshal() - return nil

I get normal slice a bytes from base (slice maked by json.Marshal) and try decode them, but json.unmarshal() - return nil

Code :

coded := redis.LoadFromBase()
uncoded := json.Unmarshal(coded, &p)

fmt.Println("Bytes:", coded)
fmt.Println("Unmarshalled:", uncoded)

Returned:

Bytes: [123 34 84 105 116 108 101 34 58 34 97 34 44 34 67 111 110 116 101 110 116 34 58 34 98 34 125]
Unmarshalled: <nil>

LoadFromBase() works fine

You are printing the error returned by json.Unmarshal not the actual decoded value. It is nil so everything is fine.

It should be:

coded := redis.LoadFromBase()
err := json.Unmarshal(coded, &p)
if (err != nil) {
    // handle error here
}

fmt.Println("Bytes:", coded)
fmt.Println("Unmarshalled:", p)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM