【发布时间】:2020-06-30 13:33:04
【问题描述】:
这是一个从 Java 处理教程复制的示例,希望在 Python 处理中有类似的东西。
String[] species = { "Capra hircus", "Panthera pardus", "Equus zebra" };
String[] names = { "Goat", "Leopard", "Zebra" };
JSONArray values;
void setup() {
values = new JSONArray();
for (int i = 0; i < species.length; i++) {
JSONObject animal = new JSONObject();
animal.setInt("id", i);
animal.setString("species", species[i]);
animal.setString("name", names[i]);
values.setJSONObject(i, animal);
}
saveJSONArray(values, "data/new.json");
}
这是我已经在 Python 中尝试过的。
import json
values = JSONArray
species = [ "Capra hircus", "Panthera pardus", "Equus zebra"]
names = [ "Goat", "Leopard", "Zebra" ]
def setup():
for i in range(len(species)):
animal = JSONObject
animal.setInt("id", i)
animal.setString("species", species[i])
animal.setString("name", names[i])
values.setJSONObject(i, animal)
saveJSONArray(values, "data/new.json")
运行程序后出现以下错误:NameError: name 'JSONArray' is not defined
我知道在 Python 处理中应该有另一种声明 JSON 对象的方法,但我是 Python 新手,所以我不知道错误可能是什么。
提前致谢:)
【问题讨论】:
标签: python json processing