使用Python解析JSON:TypeError:list indices必须是整数,而不是
发布时间:2020-12-20 13:01:31 所属栏目:Python 来源:网络整理
导读:我正在使用 Python来解析特定值的一些JSON数据.具体来说,我想提出以下内容: author_id created_at 公众 Python代码看起来像; import jsonimport requests# Set the request parametersurl = 'https:MYURL.json'user = 'MY_USER'pwd = 'MY_PWD'# Do the HTTP
我正在使用
Python来解析特定值的一些JSON数据.具体来说,我想提出以下内容:
> author_id Python代码看起来像; import json import requests # Set the request parameters url = 'https:<MYURL.json' user = 'MY_USER' pwd = 'MY_PWD' # Do the HTTP get request response = requests.get(url,auth=(user,pwd)) # Check for HTTP codes other than 200 if response.status_code != 200: print('Status:',response.status_code,'Problem with the request. Exiting.') exit() # Decode the JSON response data = response.json() # Print each value field_list = data['audits'] for fields in field_list: print(fields['author_id']) print(fields['created_at']) print(fields['events']['public']) print 'n' 我的代码错误: File "get_ticket_updates.py",line 27,in <module> print(fields['events']['public']) TypeError: list indices must be integers,not str 我得到public的值是一个字符串,它需要是整数所以,我怎么能用这个? 数据看起来像: { "audits": [ { "id": 20994687984,"ticket_id": ####,"created_at": "2014-09-15T16:30:11Z","author_id": 312016568,"via": { "channel": "email","source": { "from": { "address": "email@domain.com","name": "user name","original_recipients": [ "email@domain.com","email@domain.com" ] },"to": { "address": "email@domain.com","name": "My Portal" },"rel": null } },},{ "id": 20994845144,"created_at": "2014-09-15T16:32:18Z","author_id": 233915468,"via": { "channel": "web","source": { "from": {},"to": {},"events": [ { "id": 20994845154,"type": "Comment","body": "<SOME TEXT>","public": true,"attachments": [] }, 解决方法
字段[‘events’] [‘public’]的内容应该是字段[‘events’] [0] [‘public’]
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python – IRC bot无法加入频道
- python – openpyxl:merged_cell_ranges和merged_cells都是
- splinter WebDriverException: Message: 'chromedriver
- python中logging库的使用总结
- Python win32com用Bloomberg插件打开Excel
- python正则表达式判断字符串是否是全部小写示例
- python – 如何通过psutil获取磁盘IO和网络使用百分比
- 04 . Python入门之条件语句
- Python基础—01-认识python,编写第一个程序
- python – 函数不返回变量的随机整数