【发布时间】:2015-11-04 15:27:33
【问题描述】:
首先,我对 JSON 的术语没有完全理解,所以请耐心等待。
我的学校使用名为 PowerSchool 的成绩册系统。作为一个附带项目,我正在尝试在我的 Mac 上进行启动栏操作,以便快速查看成绩。我正在使用和服从 PowerSchool 中获取成绩。它给了我一个 JSON 输出,Python 可以抓取和输出。目前,我正在尝试能够列出所有类。相反,它列出了第一个。这是我从和服得到的 JSON 的编辑版本。 (我编辑了个人信息,只在编辑中放置了 3 个类,可能不是正确的 JSON)
name": "Power School",
"count": 10,
"frequency": "Every 15 mins",
"version": 1,
"newdata": true,
"lastrunstatus": "success",
"thisversionstatus": "success",
"nextrun": "Wed Nov 04 2015 14:02:11 GMT+0000 (UTC)",
"thisversionrun": "Wed Nov 04 2015 13:47:11 GMT+0000 (UTC)",
"results": {
"collection1": [
{
{
"property2": "Class 1",
"Q1 Grade": {
"href": "https://powerschool.url",
"text": "A+\n100"
},
"Q2 Grade": {
"href": "https://powerschool.url",
"text": "--"
},
"Q3 Grade": {
"href": "https://powerschool.url",
"text": "--"
},
"Q4 Grade": {
"href": "https://powerschool.url",
"text": "--"
},
"Y1 Grade": {
"href": "https://powerschool.url",
"text": "A+\n100"
},
"Absences": "0",
"Tardies": "0",
"Period": "1-1(A-F)",
"index": 1,
"url": "https://powerschool.url/home.html"
},
{
"property2": "Class 2",
"Q1 Grade": {
"href": "https://powerschool.url",
"text": "A+\n100"
},
"Q2 Grade": {
"href": "https://powerschool.url",
"text": "--"
},
"Q3 Grade": {
"href": "https://powerschool.url",
"text": "--"
},
"Q4 Grade": {
"href": "https://powerschool.url",
"text": "--"
},
"Y1 Grade": {
"href": "https://powerschool.url",
"text": "A+\n100"
},
"Absences": "0",
"Tardies": "0",
"Period": "1-1(A-F)",
"index": 2,
"url": "https://powerschool.url/home.html"
},
{
"property2": "Class 2",
"Q1 Grade": {
"href": "https://powerschool.url",
"text": "A+\n100"
},
"Q2 Grade": {
"href": "https://powerschool.url",
"text": "--"
},
"Q3 Grade": {
"href": "https://powerschool.url",
"text": "--"
},
"Q4 Grade": {
"href": "https://powerschool.url",
"text": "--"
},
"Y1 Grade": {
"href": "https://powerschool.url",
"text": "A+\n100"
},
"Absences": "0",
"Tardies": "0",
"Period": "2-2(A-F)",
"index": 3,
"url": "https://powerschool.url/home.html"
}
]
}
}
至于我的 python 脚本(再次删除 API 和 Auth 密钥)
import json
import urllib2
request = urllib2.Request("https://www.kimonolabs.com/api/APIKEY", headers={"authorization" : "auth code"})
contents = urllib2.urlopen(request).read()
parsed_json = json.loads(contents)
ourResult = parsed_json['results']['collection1'][0]
print ourResult['property2']
运行它会产生输出
Class 1
正如我期望的输出
Class 1
Class 2
Class 3
我错过了什么?我假设这是基本的东西。
【问题讨论】: