python - 转换为字符串json后无法加载字节Json
问题描述
它在消息中接收的 Json 是一个字节 json,如下所示:b'{"_timestamp": 1636472787, "actual": 59.9, "target": 60.0}'
代码应该将字节 Json 更改为 String Json 并加载它以访问项目,但是当我加载它时,我收到以下错误:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
代码:
import json
def handle_msg(topic, message):
m = message.decode("Utf-8")
print(json.loads(m))
解决方案
发生这种情况是因为您的消息是空值而不是您预期的如果您编写以下内容它将为您工作以下为我运行
message = b'{"_timestamp": 1636472787, "actual": 59.9, "target": 60.0}'
topic ="what ever"
import json
def handle_msg(topic, message):
m = message.decode("Utf-8")
print(json.loads(m))
handle_msg(topic, message)
推荐阅读
- regex - 如何在正则表达式中区分具有相同格式的捕获组
- haskell - 在 Haskell 服务端中进行客户端绑定 - ConnectionError
- angular - 如何在 NgFor 循环中以角材料的每行显示最多 5 张卡片
- c# - Winforms:运行 oledb 连接时出错(可安装的 Isam,未为命令对象设置 Commandtext)
- uwp - UWP C# UART RS485 通信问题
- c - 带有管道到文件的 Echo 服务器
- vuejs2 - 防止 el-menu-item 在单击时添加“is-active”类
- tar - tar.bz2 无法统计:没有这样的文件或目录
- node.js - Angular 通用 - 运行 node.js 时出现警告
- php - 如何在 laravel spatie 中更新/编辑 custom_properties?