![](/img/trans.png)
[英]Why does json.Unmarshal return a map instead of the expected struct?
[英]json.unmarshal() - return nil
我從基數(由json.Marshal制作的切片)中獲取了一個普通切片,並嘗試對其進行解碼,但是json.unmarshal()-返回nil
代碼:
coded := redis.LoadFromBase()
uncoded := json.Unmarshal(coded, &p)
fmt.Println("Bytes:", coded)
fmt.Println("Unmarshalled:", uncoded)
回來:
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()工作正常
您正在打印json.Unmarshal
而不是實際的解碼值返回的錯誤。 nil
所以一切都很好。
它應該是:
coded := redis.LoadFromBase()
err := json.Unmarshal(coded, &p)
if (err != nil) {
// handle error here
}
fmt.Println("Bytes:", coded)
fmt.Println("Unmarshalled:", p)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.