使用 JSON 函数需要导入 json 库:import json。
json.dumps:将 Python 对象编码成 JSON 字符串
例子:
python 原始类型向 json 类型的转化对照表:
pytthon | json |
---|---|
dict | object |
list,tuple | array |
str,unicode | string |
int, long, float | number |
True | true |
False | false |
None | null |
json.loads:用于解码 JSON 数据。该函数返回 Python 字段的数据类型
例子:
json 类型转换到 python 的类型对照表:
json | python |
---|---|
object | dict |
array | list |
string | unicode |
number (int) | int, long |
number (real) | float |
true | True |
false | False |
null | None |