json.unmarshal() - 返回 nil

标签 json go

我从基础(由 json.Marshal 制作的 slice )中获取一个字节的普通 slice 并尝试解码它们,但是 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)

关于json.unmarshal() - 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28264343/

相关文章:

javascript - 将多个 DataTable 序列化为 JSON

javascript - 使用 Javascript 打印 JSON 数据

javascript - angularjs ng-repeat 来自 json 嵌套的下拉菜单

windows - 如何在 Go 中获取 Windows 进程的句柄?

go - 函数调用中的"used as value"

go - 在一个变量中存储 2 个值

javascript - 使用 jQuery 读取 JSON 并通过 ID 匹配字段

multithreading - Golang : How to capture return values of massively parallel benchmark (> 1 million tasks)?

go - Google pubsub无效字体在Golang中不起作用

java - Gson:解析非标准JSON格式