Read the local json file and parse the json of instance instructions

  • 2020-06-15 09:19:43
  • OfStack

Simulated user login


# data.json  The files are in the same directory 
 
[
 {
 "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 #  The introduction of the module 
 
count = 1
 
#  Open the 1 a json file 
data = open("./data.json", encoding='utf-8')
#  convert python object 
strJson = json.load(data)
flag = False
lockFlag = False
while count <= 3:
 username = input(" Please enter the user name :")
 password = input(' Please enter your password. :')
 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(' Login successful !!')
  break
 else:
  print(' Incorrect account or password ')
 count = count + 1
#  The output ( Notice the multilayer structure )
# print(strJson[0]['id'])
# for item in strJson:
#  print(item)

Related articles: