猿问

json.unmarshal() - 返回零

我从基本切片(由 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() 工作正常


LEATH
浏览 158回答 1
1回答

收到一只叮咚

您正在打印json.Unmarshal不是实际解码值返回的错误。正是nil如此,一切都很好。它应该是:coded := redis.LoadFromBase()err := json.Unmarshal(coded, &p)if (err != nil) {&nbsp; &nbsp; // handle error here}fmt.Println("Bytes:", coded)fmt.Println("Unmarshalled:", p)
随时随地看视频慕课网APP

相关分类

Go
我要回答