import orjson
if __name__ == '__main__': listVar = [1, 2, 3, 4, 5, 6, 7, 8, 9] listJson = orjson.dumps(listVar) print('列表转json:', listJson, 'type:', type(listJson)) print('结果bytes转str:', listJson.decode('utf-8'))
mapVar = {'name': '张三', 'age': 18} mapJson = orjson.dumps(mapVar) print('map转json:', mapJson, 'type:', type(mapJson)) print('结果bytes转str:', mapJson.decode('utf-8'))
listMapVar = [ {'name': '张三', 'age': 18}, {'name': '李四', 'age': 33}, {'name': '王五', 'age': 20}, ] listMapVarJson = orjson.dumps(listMapVar) print('列表map转json:', listMapVarJson, 'type:', type(listMapVarJson)) print('结果bytes转str:', listMapVarJson.decode('utf-8'))
列表转json: b'[1,2,3,4,5,6,7,8,9]' type: <class 'bytes'> 结果bytes转str: [1,2,3,4,5,6,7,8,9] map转json: b'{"name":"\xe5\xbc\xa0\xe4\xb8\x89","age":18}' type: <class 'bytes'> 结果bytes转str: {"name":"张三","age":18} 列表map转json: b'[{"name":"\xe5\xbc\xa0\xe4\xb8\x89","age":18},{"name":"\xe6\x9d\x8e\xe5\x9b\x9b","age":33},{"name":"\xe7\x8e\x8b\xe4\xba\x94","age":20}]' type: <class 'bytes'> 结果bytes转str: [{"name":"张三","age":18},{"name":"李四","age":33},{"name":"王五","age":20}]
|