当前位置:主页 > 软件编程 > Python代码 >

读取本地json文件,解析json(实例讲解)

时间:2020-11-10 15:25:01 | 栏目:Python代码 | 点击:

模拟用户登录

# data.json 文件同目录下
 
[
 {
 "id": 1,
 "username": "zhangshan",
 "password": "123qwe",
 "lock": false
 },
 {
 "id": 2,
 "username": "lisi",
 "password": "123qwe",
 "lock": false
 },
 {
 "id": 3,
 "username": "wangwu",
 "password": "123qwe",
 "lock": false
 }
]
import json # 引入模块
 
count = 1
 
# 打开一个json文件
data = open("./data.json", encoding='utf-8')
# 转换为python对象
strJson = json.load(data)
flag = False
lockFlag = False
while count <= 3:
 username = input("请输入用户名:")
 password = input('请输入密码:')
 for user in strJson:
  if username == user['username'] and password == user['password']:
   if not user['lock']:
    flag = True
   else:
    lockFlag = True
   break
 if flag:
  print('登录成功!!')
  break
 else:
  print('账号或密码错误')
 count = count + 1
# 输出(注意多层结构)
# print(strJson[0]['id'])
# for item in strJson:
#  print(item)

您可能感兴趣的文章:

相关文章