读取本地json文件,解析json(实例讲解)
模拟用户登录
# 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)
以上这篇读取本地json文件,解析json(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。