用串口读取JSON数据,解析时报错,请问是什么原因?
-
用串口读取:{'a':'1111','b':'2222','c':'3333','d':'4444'} 时,JSON解析时报错,请问是什么原因?
程序如下:import time, pyb from pyb import UART import ujson led = pyb.LED(3) uart = UART(1, 9600) while(True): if(uart.any()): # 是否有数据 time.sleep(200) led.off() d = uart.read() da = str(d, "utf-8") # bytes转str dd = eval((da)) jsObj = ujson.loads(dd) print(type(jsObj)) for key in jsObj.keys(): print('key: %s value: %s' % (key,jsObj.get(key))) else: led.on() time.sleep(1000)
Traceback (most recent call last):
File "", line 15, in
TypeError: can't convert 'dict' object to str implicitly
MicroPython v1.9.4-4510-g23e8457de on 2018-06-29; OPENMV3 with STM32F765
Type "help()" for more information.哪位大师能给出解决方案,谢谢!
-
通过自己摸索,已自行解决了!
-
json标准里,字符串只能用双引号,而不是单引号
s = '{"a":"1111","b":"2222","c":"3333","d":"4444"}' json.loads(s)