读取 json 文件
# 读取 json 文件
import json with open("./data_file/user_info.json", "r") as f: data = f.read() userList = json.loads(data) print(userList) for userInfo in userList: print(userInfo) print(userInfo['username']) print(userInfo['password']) print("***********")
'''
json文件如下:
————————————————————————————————————————
[
{"username":"", "password":""},
{"username":"", "password":"123"},
{"username":"root", "password":"123456"},
{"username":"admin", "password":"admin"}
]
——————————————————————————————————————————————————
JSON 是一种轻量级的数据交换格式,清晰的层次结构使得 JSON 文件被广泛使用,
Python 同样可以读取 JSON 文件。
open() 方法 读取 json 文件,因为测试数据本身是以列表和字典的格式存放的,
所以读取整个文件后, 通过 JSON 提供的表将 str 类型转换为 list 类型即可
'''
浙公网安备 33010602011771号