我一直在寻找其他的答案,比如说(this),但运气不好。我正在编写一个脚本来从API中提取数据。你知道吗
我想循环遍历结果字典并提取每个结果的细节,例如名称、总体等json.dumps文件这是:
{
"count": 61,
"next": "https://swapi.co/api/planets/?page=2",
"previous": null,
"results": [
{
"climate": "temperate",
"created": "2014-12-10T11:35:48.479000Z",
"diameter": "12500",
"edited": "2014-12-20T20:58:18.420000Z",
"films": [
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/1/"
],
"gravity": "1 standard",
"name": "Alderaan",
"orbital_period": "364",
"population": "2000000000",
"residents": [
"https://swapi.co/api/people/5/",
"https://swapi.co/api/people/68/",
"https://swapi.co/api/people/81/"
],
"rotation_period": "24",
"surface_water": "40",
"terrain": "grasslands, mountains",
"url": "https://swapi.co/api/planets/2/"
},
{
"climate": "temperate, tropical",
"created": "2014-12-10T11:37:19.144000Z",
"diameter": "10200",
"edited": "2014-12-20T20:58:18.421000Z",
"films": [
"https://swapi.co/api/films/1/"
],
"gravity": "1 standard",
"name": "Yavin IV",
"orbital_period": "4818",
"population": "1000",
"residents": [],
"rotation_period": "24",
"surface_water": "8",
"terrain": "jungle, rainforests",
"url": "https://swapi.co/api/planets/3/"
},
{
"climate": "frozen",
"created": "2014-12-10T11:39:13.934000Z",
"diameter": "7200",
"edited": "2014-12-20T20:58:18.423000Z",
"films": [
"https://swapi.co/api/films/2/"
],
"gravity": "1.1 standard",
"name": "Hoth",
"orbital_period": "549",
"population": "unknown",
"residents": [],
"rotation_period": "23",
"surface_water": "100",
"terrain": "tundra, ice caves, mountain ranges",
"url": "https://swapi.co/api/planets/4/"
},
对Python还不熟悉,所以如果有更好的方法,我愿意接受建议!你知道吗
在我的测试代码中,我有:
print(jsonResponse["results"][1]["name"])
我可以手动将1改为其他数字,并得到正确的名称打印出来。你知道吗
如果我使用这个,我会得到“list index must be integers or slices,not str”消息
for i in jsonResponse:
print(jsonResponse["results"][i]["name"])
jsonResponse["results"]
中的元素,而不是jsonResponse
。你知道吗这样你就可以:
相关问题 更多 >
编程相关推荐