Parse data from multiple numbers in a list or slice - TypeError: list indices must be integers or slices, not str
Parse data from multiple numbers in a list or slice - TypeError: list indices must be integers or slices, not str
问题
我正在尝试从 freshdesk 获取所有支持工单的状态列表。
到目前为止,我已经能够 request.get 所有门票,但它包含的信息太多了。所以我试图只从 json 数据中提取“状态”。
如果我只想打印一张票的信息,会发生以下情况:
ticketresponse = requests.get(freshdeskticketsurl, auth = (freshdeskapi, freshdeskpassword))
ticket_json = ticketresponse.json()
ticket_str = json.dumps(ticket_json[0], indent=2)
print(ticket_str)
回复:
{
"cc_emails": [],
"fwd_emails": [],
"reply_cc_emails": [],
"ticket_cc_emails": [],
"fr_escalated": false,
"spam": false,
"email_config_id": null,
"group_id": 43000110950,
"priority": 1,
"requester_id": taking that out of this example,
"responder_id": taking that out of this example,
"source": 3,
"company_id": taking that out of this example,
"status": 5,
"subject": "Receptiontv-min - can't use slide show in powerpoint",
"association_type": null,
"support_email": null,
"to_emails": null,
"product_id": null,
"id": taking that out of this example,
"type": "Trouble Ticket",
"due_by": "2021-03-09T19:21:09Z",
"fr_due_by": "2022-02-25T19:21:09Z",
"is_escalated": false,
"custom_fields": {},
"created_at": "2021-03-02T19:21:09Z",
"updated_at": "2021-03-02T19:21:16Z",
"associated_tickets_count": null,
"tags": []
}
我只想要“状态” 部分。我可以通过这样做只用一张票来做到这一点:
ticketresponse = requests.get(freshdeskticketsurl, auth = (freshdeskapi, freshdeskpassword))
ticket_json = ticketresponse.json()
ticket_str = json.dumps(ticket_json[0]['status'], indent=2)
print(ticket_str)
回复:
5
但是,我遇到问题的地方是尝试一次打印出所有票证的状态。我可以使用 ticket_str = json.dumps(ticket_json, indent=2) print(ticket_str)
一次打印出所有的票
但是,如果我尝试使用以下任何代码行来尝试获取所有状态,它会给我同样的错误:
ticket_str = json.dumps(ticket_json['status'], indent=2)
响应
TypeError: list indices must be integers or slices, not str
ticketslice = slice(0,20) ticket_str = json.dumps(ticket_json[ticketslice]['status'], indent=2)
响应
TypeError: list indices must be integers or slices, not str
如果有人有任何想法或有不同的想法,请告诉我。
感谢您的宝贵时间,
如果你想要所有你需要在响应数组上循环的状态,像这样:
for i in range(len(ticket_json)):
ticket_str = json.dumps(ticket_json[i], indent=2)
print(ticket_str['status'])
问题
我正在尝试从 freshdesk 获取所有支持工单的状态列表。
到目前为止,我已经能够 request.get 所有门票,但它包含的信息太多了。所以我试图只从 json 数据中提取“状态”。
如果我只想打印一张票的信息,会发生以下情况:
ticketresponse = requests.get(freshdeskticketsurl, auth = (freshdeskapi, freshdeskpassword))
ticket_json = ticketresponse.json()
ticket_str = json.dumps(ticket_json[0], indent=2)
print(ticket_str)
回复:
{
"cc_emails": [],
"fwd_emails": [],
"reply_cc_emails": [],
"ticket_cc_emails": [],
"fr_escalated": false,
"spam": false,
"email_config_id": null,
"group_id": 43000110950,
"priority": 1,
"requester_id": taking that out of this example,
"responder_id": taking that out of this example,
"source": 3,
"company_id": taking that out of this example,
"status": 5,
"subject": "Receptiontv-min - can't use slide show in powerpoint",
"association_type": null,
"support_email": null,
"to_emails": null,
"product_id": null,
"id": taking that out of this example,
"type": "Trouble Ticket",
"due_by": "2021-03-09T19:21:09Z",
"fr_due_by": "2022-02-25T19:21:09Z",
"is_escalated": false,
"custom_fields": {},
"created_at": "2021-03-02T19:21:09Z",
"updated_at": "2021-03-02T19:21:16Z",
"associated_tickets_count": null,
"tags": []
}
我只想要“状态” 部分。我可以通过这样做只用一张票来做到这一点:
ticketresponse = requests.get(freshdeskticketsurl, auth = (freshdeskapi, freshdeskpassword))
ticket_json = ticketresponse.json()
ticket_str = json.dumps(ticket_json[0]['status'], indent=2)
print(ticket_str)
回复:
5
但是,我遇到问题的地方是尝试一次打印出所有票证的状态。我可以使用 ticket_str = json.dumps(ticket_json, indent=2) print(ticket_str)
但是,如果我尝试使用以下任何代码行来尝试获取所有状态,它会给我同样的错误:
ticket_str = json.dumps(ticket_json['status'], indent=2)
响应
TypeError: list indices must be integers or slices, not str
ticketslice = slice(0,20) ticket_str = json.dumps(ticket_json[ticketslice]['status'], indent=2)
响应
TypeError: list indices must be integers or slices, not str
如果有人有任何想法或有不同的想法,请告诉我。
感谢您的宝贵时间,
如果你想要所有你需要在响应数组上循环的状态,像这样:
for i in range(len(ticket_json)):
ticket_str = json.dumps(ticket_json[i], indent=2)
print(ticket_str['status'])