json API数据中的datetime keyerror

时间:2018-04-01 06:00:51

标签: json python-3.5

使用Python 3.5,我正在尝试从Todoist REST api返回数据,这是一种JSON格式。

  
    

[{'id':2577166691,'project_id':2181643136,'url':'https://todoist.com/showTask?id=2577166691','已完成':错误,'订单':2,'内容':'很快',' priority':1,'comment_count':0,'due':{'recurring':False,'date':'2018-04-01','timezone':'UTC + 10:00','datetime': '2018-04-01T10:00:00Z','字符串':'2019年3月31日'},'缩进':1},{'id':2577166849,'project_id':2181643136,'url':'{{ 3}}','完成':错误,'顺序':3,'内容':'到城市+1','优先':1,'comment_count':0,'到期':{'重复':错误,'date':'2018-03-31','string':'Mar 31'},'indent':1},{'id':2577225965,'project_id':2181643136,'url':'{{ 3}}','完成':False,'order':4,'content':'To City +2','priority':1,'comment_count':0,'indent':1},{'id ':2577974095,'project_id':2181643136,'url':'https://todoist.com/showTask?id=2577166849','已完成':错误,'订单':5,'内容':'到城市+3','优先':1 ,'comment_count':0,'缩进':1},{'id':2577974970,'project_id':2181643136,'url':'https://todoist.com/showTask?id=2577225965','已完成':错误,'顺序':6 ,'内容':'来自城市的下一班火车' ,'priority':1,'comment_count':0,'indent':1},{'id':2577975012,'project_id':2181643136,'url':'https://todoist.com/showTask?id=2577974095','completed':False ,'order':'from City +1','priority':1,'comment_count':0,'indent':1},{'id':2577975101,'project_id':2181643136, 'url':'https://todoist.com/showTask?id=2577974970','已完成':错误,'订单':8,'内容':'来自城市+2','优先':1,'comment_count':0,'缩进' :1},{'id':2577975145,'project_id':2181643136,'url':'https://todoist.com/showTask?id=2577975012','已完成':错误,'顺序':9,'内容':'来自城市+3 ','priority':1,'comment_count':0,'indent':1}]

  

我可以正确获取所有项目的数据,例如

print(json_tasks[0]['id']

  
    

2577166691

  

它也适用于

print(json_tasks[0]['due']['recurring'])

  
    

  

print(json_tasks[0]['due']['date'])

  
    

2018年4月1日

  

可是:

print(json_tasks[0]['due']['datetime'])

  
    

'KeyError:'datetime'

  

我尝试了很多东西,但我很难过。我究竟做错了什么?如何才能将'datetime'视为关键?

1 个答案:

答案 0 :(得分:0)

下面的代码,当我运行它时,打印出2018-04-01T10:00:00Z

json_tasks = [{'id': 2577166691, 'project_id': 2181643136, 'url': 'https://todoist.com/showTask?id=2577166691', 'completed': False, 'order': 2, 'content': 'soon', 'priority': 1, 'comment_count': 0, 'due': {'recurring': False, 'date': '2018-04-01', 'timezone': 'UTC+10:00', 'datetime': '2018-04-01T10:00:00Z', 'string': 'Mar 31 2019'}, 'indent': 1}, {'id': 2577166849, 'project_id': 2181643136, 'url': 'https://todoist.com/showTask?id=2577166849', 'completed': False, 'order': 3, 'content': 'To City +1', 'priority': 1, 'comment_count': 0, 'due': {'recurring': False, 'date': '2018-03-31', 'string': 'Mar 31'}, 'indent': 1}, {'id': 2577225965, 'project_id': 2181643136, 'url': 'https://todoist.com/showTask?id=2577225965', 'completed': False, 'order': 4, 'content': 'To City +2', 'priority': 1, 'comment_count': 0, 'indent': 1}, {'id': 2577974095, 'project_id': 2181643136, 'url': 'https://todoist.com/showTask?id=2577974095', 'completed': False, 'order': 5, 'content': 'To City +3', 'priority': 1, 'comment_count': 0, 'indent': 1}, {'id': 2577974970, 'project_id': 2181643136, 'url': 'https://todoist.com/showTask?id=2577974970', 'completed': False, 'order': 6, 'content': 'Next train from City', 'priority': 1, 'comment_count': 0, 'indent': 1}, {'id': 2577975012, 'project_id': 2181643136, 'url': 'https://todoist.com/showTask?id=2577975012', 'completed': False, 'order': 7, 'content': 'From City +1', 'priority': 1, 'comment_count': 0, 'indent': 1}, {'id': 2577975101, 'project_id': 2181643136, 'url': 'https://todoist.com/showTask?id=2577975101', 'completed': False, 'order': 8, 'content': 'From City +2', 'priority': 1, 'comment_count': 0, 'indent': 1}, {'id': 2577975145, 'project_id': 2181643136, 'url': 'https://todoist.com/showTask?id=2577975145', 'completed': False, 'order': 9, 'content': 'From City +3', 'priority': 1, 'comment_count': 0, 'indent': 1}]

print(json_tasks[0]['due']['datetime'])