下面是我的 JSON 文件的样子,我在这里不断地观看它。我正在观看这个文件,因为我想检索 Python 中附加到 JSON 数组的每个新项目。
我想知道什么是更好的方法。
[
{key1:value1,key2:value2},
{key1:value1,key2:value2}
]
我想说这种方式可能是一个好方法:
import json
# path to the JSON file
json_file_path = 'your_file.json'
# Read the JSON content from the file
with open(json_file_path, 'r') as file:
data = json.load(file)
# Check the data received is a list
if isinstance(data, list):
# Check that it's not empty
if data:
# Get last item
last_item = data[-1]
print(last_item)
else:
print("The JSON array is empty.")
else:
print("The JSON data is not in the expected format (list of dictionaries).")