可以使用json库中的loads函数将JSON字符串转化为Python对象,然后使用list()函数将Python对象转化为数组。下面是一个示例:
import json json_str = '[1, 2, 3, 4, 5]' json_list = json.loads(json_str) array = list(json_list) print(array) 输出结果为:
[1, 2, 3, 4, 5] 你也可以直接使用json库中的load函数从文件中读取JSON数据,然后将其转化为数组。下面是一个示例:
import json with open('data.json') as file: json_list = json.load(file) array = list(json_list) print(array) 假设data.json文件中的内容为[1, 2, 3, 4, 5],输出结果为:
[1, 2, 3, 4, 5]