它在訊息中接收的 Json 是一個位元組 json,如下所示:b'{"_timestamp": 1636472787, "actual": 59.9, "target": 60.0}'
代碼應該將位元組 Json 更改為字串 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))
uj5u.com熱心網友回復:
發生這種情況是因為您的訊息是空值,而不是您所期望的,如果您撰寫以下內容,它將為您作業
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/354722.html
