在Python中导航JSON?
答:数据[数据][项目]中的方括号表示词典列表。我认为括号表示所有这些词典都是一个项目列表中的一个列表,即[item1,item2]],因此我必须调用0索引,如:data[data][items][0]。这是不正确的,[0]是不必要的,我真的应该知道,但没有意识到 我使用的是GAE,所以您可以将self.response.write语句视为打印 出于某种原因,尽管用户_xid打印正确,但在我尝试解析For循环中的json_data[data][items][0]时,解释器给了我错误,它似乎将x作为字符串计算,因为我收到一个错误,告诉我字符串标记必须是整数 我没有意识到json_data[data][items][0]会生成字符串。我有点碍手碍脚。它是否与使用json_数据[data][items][0]而不是json_数据[data][items]有关 我还尝试了json.loadsjson_data[data][items][0]但没有成功。欢迎您的任何意见 我有以下JSON,我对其进行了一些扩展,以使其更易于阅读:在Python中导航JSON?,python,json,google-app-engine,Python,Json,Google App Engine,答:数据[数据][项目]中的方括号表示词典列表。我认为括号表示所有这些词典都是一个项目列表中的一个列表,即[item1,item2]],因此我必须调用0索引,如:data[data][items][0]。这是不正确的,[0]是不必要的,我真的应该知道,但没有意识到 我使用的是GAE,所以您可以将self.response.write语句视为打印 出于某种原因,尽管用户_xid打印正确,但在我尝试解析For循环中的json_data[data][items][0]时,解释器给了我错误,它似乎将x作
{"meta":
{"user_xid": "REDACTED", "message": "OK", "code": 200, "time": 1436273692},
"data":
{"items": [
{"time_completed": 1436193938, "xid": "4tLz2M3x8_E9mTPIQsQUdrj-83spikha", "title": "for 7h 40m", "time_created": 1436165067, "time_updated": 1436194828, "details":
{"body": 0, "sound": 14348, "tz": "America/Vancouver", "awakenings": 1, "light": 13258, "mind": 0, "asleep_time": 1436165760, "awake": 1265, "rem": 0, "duration": 28871, "smart_alarm_fire": 1436193000, "quality": 81, "awake_time": 1436193600, "sunrise": 1436184840, "sunset": 1436156340},
"date": 20150706, "shared": true, "sub_type": 0},
{"time_completed": 1436135400, "xid": "4tLz2M3x8_Hk71lnuSLbeq4ascwJg-J8", "title": "for 25m", "time_created": 1436133600, "time_updated": 1436135590, "details":
{"body": 0, "sound": 0, "tz": "America/Vancouver", "awakenings": 0, "light": 1500, "mind": 0, "asleep_time": 1436133600, "awake": 300, "rem": 0, "duration": 1800, "smart_alarm_fire": 0, "quality": 2, "awake_time": 1436134800, "sunrise": 0, "sunset": 0},
"date": 20150705, "shared": true, "sub_type": 2},
{"time_completed": 1436133497, "xid": "4tLz2M3x8_GIYf6G9_ZKH9xrKpuEzMjm", "title": "for 23m", "time_created": 1436131387, "time_updated": 1436135461, "details":
{"body": 0, "sound": 1391, "tz": "America/Vancouver", "awakenings": 0, "light": 0, "mind": 0, "asleep_time": 1436132100, "awake": 719, "rem": 0, "duration": 2110, "smart_alarm_fire": 1436133445, "quality": 4, "awake_time": 1436133300, "sunrise": 0, "sunset": 0},
"date": 20150705, "shared": true, "sub_type": 1},
{"time_completed": 1436117080, "xid": "4tLz2M3x8_EUZyqCoXYOJO7Hfe92eSqg", "title": "for 26m","time_created": 1436115061, "time_updated": 1436121619, "details":
{"body": 0, "sound": 480, "tz": "America/Vancouver", "awakenings": 0, "light": 1119, "mind": 0, "asleep_time": 1436115479, "awake": 420, "rem": 0, "duration": 2019, "smart_alarm_fire": 1436117069, "quality": 2, "awake_time": 1436116800, "sunrise": 1436098380, "sunset": 1436156280},
"date": 20150705, "shared": true, "sub_type": 1},
],
"links":
{"next": "REDACTED"},
"size": 10}
}
我在Python中使用以下内容:
json_data = json.loads(user_data)
self.response.write('<br><br>x_id: ' + json_data["meta"]["user_xid"])
i = 0
for x in json_data["data"]["items"][0]:
duration = x['details']['duration']
date = x["date"]
self.response.write('<br><br>sleep number ' + str(i) + ' | duration: ' + str(duration) + ' | date: ' + str(date))
i += 1
我使用的是GAE,所以您可以将self.response.write语句视为打印
出于某种原因,尽管用户_xid打印正确,但在我尝试解析For循环中的json_data[data][items][0]时,解释器给了我错误,它似乎将x作为字符串计算,因为我收到一个错误,告诉我字符串标记必须是整数
我没有意识到json_data[data][items][0]会生成字符串。我有点碍手碍脚。它是否与使用json_数据[data][items][0]而不是json_数据[data][items]有关
我还尝试了json.loadsjson_data[data][items][0]但没有成功。欢迎您的任何意见
谢谢 通过一些基本的调试和数据检查,您会发现[0]的使用是不正确的 我很快加载了数据,并进行了尝试
In [11]: for i in x['data']['items'][0]:
....: print i
....:
time_completed
xid
details
title
date
shared
sub_type
time_created
time_updated
这实际上是你得到的,但不是你期望的
那么到底发生了什么?如果您执行以下操作:
x['data']['items'][0]
Out[28]:
{'date': 20150706,
'details': {'asleep_time': 1436165760,
'awake': 1265,
'awake_time': 1436193600,
'awakenings': 1,
'body': 0,
'duration': 28871,
'light': 13258,
'mind': 0,
'quality': 81,
'rem': 0,
'smart_alarm_fire': 1436193000,
'sound': 14348,
'sunrise': 1436184840,
'sunset': 1436156340,
'tz': 'America/Vancouver'},
'shared': True,
'sub_type': 0,
'time_completed': 1436193938,
'time_created': 1436165067,
'time_updated': 1436194828,
'title': 'for 7h 40m',
'xid': '4tLz2M3x8_E9mTPIQsQUdrj-83spikha'}
结果正如你所期望的
答案是因为您正在尝试迭代x['data']['items'][0]
当您在字典上迭代时,默认情况下会得到键而不是值。因此,您将获得意外的字符串
下面的代码将为您提供所需的内容
In [30]: for i in x['data']['items']:
print i['details']['duration']
....:
28871
1800
2110
2019
这将在项目列表中的每个项目上迭代一个词汇表
我认为否决票是由于大量代码发布,并且没有基本调试的真实证据。为什么否决?谢谢!我一直在试着调试这个,但我是一个相当大的新手。太好了,我试过了,它很有效。我以前没有真正使用过JSON,我试着将它加载到解释器中测试数据,但实际上我不知道如何实现这一点,我对这方面非常陌生。json_data[data][item]生成字典列表,其中每个字典中的第一个键都是完成时间。。。钥匙里的方形咯咯声把我吓坏了。再次感谢!没问题。不过,请注意,问题与json无关,而是如何在python中使用嵌套的dctionaries和list。