模拟用户登录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 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 } ] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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) |
以上这篇读取本地json文件,解析json(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/412013cl/archive/2017/12/05/7989874.html