python json解析 建索引_使用Python解析JSON:TypeError:列表索引必须是整数,而不是str...

我正在使用Python通过一些JSON数据来解析特定的值。具体来说,我想提出以下几点:

author_id

created_at

上市

Python代码看起来像;

import json

import requests

# Set the request parameters

url = 'https:

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

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,

"ticket_id": ####,

"created_at": "2014-09-15T16:32:18Z",

"author_id": 233915468,

"via": {

"channel": "web",

"source": {

"from": {},

"to": {},

"rel": null

}

},

"events": [

{

"id": 20994845154,

"type": "Comment",

"author_id": 233915468,

"body": "",

"public": true,

"attachments": []

},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值