xiuos/Ubiquitous/RT_Thread/micropython/docs/std-librarys/ujson.md

43 lines
1.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## **ujson** JSON编码与解码
`ujson` 模块提供 Python 对象到 JSONJavaScript Object Notation 数据格式的转换。
### 函数
#### **ujson.dumps**(obj)
将 dict 类型转换成 str。
```
obj要转换的对象
```
示例:
```
>>> obj = {1:2, 3:4, "a":6}
>>> print(type(obj), obj) #原来为dict类型
<class 'dict'> {3: 4, 1: 2, 'a': 6}
>>> jsObj = json.dumps(obj) #将dict类型转换成str
>>> print(type(jsObj), jsObj)
<class 'str'> {3: 4, 1: 2, "a": 6}
```
#### **ujson.loads**(str)
解析 JSON 字符串并返回对象。如果字符串格式错误将引发 ValueError 异常。
示例:
```
>>> obj = {1:2, 3:4, "a":6}
>>> jsDumps = json.dumps(obj)
>>> jsLoads = json.loads(jsDumps)
>>> print(type(obj), obj)
<class 'dict'> {3: 4, 1: 2, 'a': 6}
>>> print(type(jsDumps), jsDumps)
<class 'str'> {3: 4, 1: 2, "a": 6}
>>> print(type(jsLoads), jsLoads)
<class 'dict'> {'a': 6, 1: 2, 3: 4}
```
更多内容可参考 [ujson](http://docs.micropython.org/en/latest/pyboard/library/ujson.html) 。