json.unmarshal() - 返回 nil

Max*_*Lis 1 json go

我从基部获取正常的字节切片(由 json.Marshal 制作的切片)并尝试解码它们,但是 json.unmarshal() - 返回 nil

代码 :

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

fmt.Println("Bytes:", coded)
fmt.Println("Unmarshalled:", uncoded)
Run Code Online (Sandbox Code Playgroud)

回到:

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>
Run Code Online (Sandbox Code Playgroud)

LoadFromBase() 工作正常

Grz*_*Żur 5

您正在打印返回的错误,而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)
Run Code Online (Sandbox Code Playgroud)