使用Python解析JSON:TypeError:list indices必须是整数,而不是str

时间:2023-01-02 17:08:01

I'm using Python to parse through some JSON data for specific values. Specifically I want to pull the following:

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

  • author_id
  • AUTHOR_ID
  • created_at
  • created_at
  • public
  • 上市

The Python code looks like;

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'

My code errors with:

我的代码错误:

File "get_ticket_updates.py", line 27, in <module>
print(fields['events']['public'])
TypeError: list indices must be integers, not str

I get that the value of public is a string and it needs to to be integer so, how can I work with this?

我得到public的值是一个字符串,它需要是整数所以,我怎么能用这个?

The data looks like:

数据如下:

{

"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": "<SOME TEXT>",
                "public": true,
                "attachments": []
            },

3 个解决方案

#1


17  

Insted of fields['events']['public'] it should be fields['events'][0]['public']

字段['events'] ['public']的内容应该是字段['events'] [0] ['public']

#2


2  

print(fields['events'][0]['public'])

fields['events'] is a list so you need to use ['events'][0] to access the dict inside the list.

fields ['events']是一个列表,因此您需要使用['events'] [0]来访问列表中的dict。

#3


2  

It's exactly as the error says. fields['events'] is a list, so you can't index it with ['public']. You need to iterate through the values, each of which is a dictionary.

正如错误所说的那样。 fields ['events']是一个列表,因此您无法使用['public']对其进行索引。您需要遍历值,每个值都是字典。

for event in fields['events']:
    print event['public']

#1


17  

Insted of fields['events']['public'] it should be fields['events'][0]['public']

字段['events'] ['public']的内容应该是字段['events'] [0] ['public']

#2


2  

print(fields['events'][0]['public'])

fields['events'] is a list so you need to use ['events'][0] to access the dict inside the list.

fields ['events']是一个列表,因此您需要使用['events'] [0]来访问列表中的dict。

#3


2  

It's exactly as the error says. fields['events'] is a list, so you can't index it with ['public']. You need to iterate through the values, each of which is a dictionary.

正如错误所说的那样。 fields ['events']是一个列表,因此您无法使用['public']对其进行索引。您需要遍历值,每个值都是字典。

for event in fields['events']:
    print event['public']