之前介绍了esp32-s3的http通信,对于返回的结果进行解析也是必须的,通常我们可以使用json格式进行通信,这样即便于理解也便于取值。今天我们介绍下JSON解析。
在这里用到的库是ujson,代码如下,将如下代码保存到设备即可
import micropython
import json
from json import dump, dumps
import io
import sys
# Before CPython3.6. only str input was acceptet by json module
_json_only_str = sys.version_info < (3, 6)
def load(stream):
if _json_only_str:
# io.TextIOWrapper doesn't support text streams as input,
# so we should pass only binary streams to it.
if not isinstance(stream, (io.TextIOWrapper, io.StringIO)):
stream = io.TextIOWrapper(stream)
return json.load(stream)
def loads(s):
if _json_only_str and not isinstance(s, str):
s = s.decode()
return json.loads(s)
保存以上代码后,进行json解