python 读写json

python 读写json

  1. 将python dict 写入json文件

    • json.dump()

    •   import json
        dict = {'a':1213,'b':['ad',3,'23fs']}     
        with open("file_path", "w") as json_f:
        	json.dump(dict, json_f)
      
    
    
  2. 读取json文件

    • json.load()
    •   import json()
        with open("file_path", "r") as json_f:
        	dict = json.load(json_f)
      
    
    
  3. 将python字典转换成json字符串

    • json.dumps()
    •   import json
        dict = {'a':1213,'b':['ad',3,'23fs']} 
        dict_str = json.dumps(dict)
      
    
    
  4. 将json字符串转换成python字典

    • json.loads()
    •   import json
        dict_str = '{'a':1213,'b':['ad',3,'23fs']}' 
        dict = json.loads(dict_str)
      
原文地址:https://www.cnblogs.com/liushi-Oscar/p/9577529.html