Python中可以使用json
模塊來讀取JSON數(shù)組。具體步驟如下:
json
模塊。open()
函數(shù)打開包含JSON數(shù)據(jù)的文件,并指定文件路徑和打開模式。json.load()
函數(shù)加載JSON數(shù)據(jù),并將其存儲在一個變量中。以下是一個示例代碼:
import json
# 打開包含JSON數(shù)據(jù)的文件
with open('data.json', 'r') as file:
# 加載JSON數(shù)據(jù)
data = json.load(file)
# 訪問JSON數(shù)組中的元素
for item in data:
print(item)
在上面的示例中,假設(shè)有一個名為data.json
的文件,其中包含一個JSON數(shù)組。通過打開文件、加載JSON數(shù)據(jù),并使用循環(huán)訪問數(shù)組中的元素,可以讀取JSON數(shù)組的內(nèi)容。