json --> str :json.dumps
str --> json: json.loads
import json # json -- > str json_obj = { "a": 1, "b": [ 1, 2, { "b1": "b2" } ] } str_1 = json.dumps(json_obj) print type(str_1) print str_1 # str -- > json json_obj_2 = json.loads(str_1) print type(json_obj_2) print json_obj_2
关于json.dump 和 json.load
# 把json对象写入文件 with open("test_loads.json", 'w') as f: json.dump(json_obj, f) # 从文件中读出json对象 with open("test_loads.json", 'r') as f: json_obj_3 = json.load(f) print type(json_obj_3) print json_obj_3