1 import json 2 3 # json.dumps 用于将 Python 对象编码成 JSON 字符串。 4 print(json.dumps([‘foo‘, {‘bar‘: (‘baz‘, None, 1.0, 2)}])) 5 print(type(json.dumps([‘foo‘, {‘bar‘: (‘baz‘, None, 1.0, 2)}]))) 6 7 # json.loads 用于解码 JSON 数据。该函数返回 Python 字段的数据类型。 8 print(json.loads(‘{"a":1,"b":2,"c":3,"d":4,"e":5}‘)) 9 print(type(json.loads(‘{"a":1,"b":2,"c":3,"d":4,"e":5}‘)))
1 ["foo", {"bar": ["baz", null, 1.0, 2]}] 2 <class ‘str‘> 3 {‘a‘: 1, ‘b‘: 2, ‘c‘: 3, ‘d‘: 4, ‘e‘: 5} 4 <class ‘dict‘>