json中loads的用法

时间:2022-05-16 20:04:04

python中json中的loads()和dumps()它们的作用经常弄换了,这里记录下,loads方法是把json对象转化为python对象,dumps方法是把pyhon对象转化为json对象,我是这么理解的,先上段loas的代码:

#!/usr/bin/env python
#coding=utf8
import os
import sys
import json
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
db_user_info = BASE_DIR + r"\database\users_dict"
username = "liuhui"
def quxian():
with open(db_user_info, "r+", encoding="utf8") as user_file:
users_info = json.loads(user_file.read())
if username in users_info.keys():
print("ok")
else:
print("wrong")
quxian()