首先,我不完全了解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
我错过了什么?我假设它是基本的东西。
答案 0 :(得分:0)
您将'collection1'的所有项目放入ourResult。 创建一个for循环,迭代myResult中的每个元素,并使用键'property2'
搜索值答案 1 :(得分:0)
您只需阅读一个项目而不是数组。试试这个:
ourResult = parsed_json['results']['collection1']
for item in ourResult:
print item['property2']