如何解析从 pyserial 接收到的 json 字符串?

How to parse json string recived from pyserial?

我使用 pyserial 库从 Arduino 获取数据。我使用以下代码:

import serial
import JSON

ser = serial.Serial('/dev/ttyUSB0', 9600)
while True:
    print(ser.readline())

当我 运行 我得到的程序: b'{"x":"-1","y":"0"}\r\n' 但我期望 {"x":"-1","y":"0"}.

我知道我可以通过在 ser,redline() 上使用 stipe() 方法来删​​除 \r\n。我还注意到 b' 来自 bytes.

当我尝试通过命令 data = json.load(ser.readline().strip()) 将字符串加载到 json 时,出现错误 AttributeError: 'bytes' object has no attribute 'read'

如何将字符串解析为 json 格式?

使用your_line.decode('utf-8')解码你的行