试图将多个项追加到列表,但获取Python TypeError:列表索引必须是整数或片,而不是str

2024-04-25 07:09:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图循环浏览customfield_10003,并为列表中的每个问题在每个customfield_10003中附加所有“name”值

以下是我正在处理的JSON:

{
        "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
        "fields": {
            "customfield_10003": [
                {
                    "boardId": 11,
                    "completeDate": "2021-03-24T16:16:31.040Z",
                    "endDate": "2021-03-23T14:38:00.000Z",
                    "goal": "",
                    "id": 34,
                    "name": "Promo Sim 3/23",
                    "startDate": "2021-03-10T14:38:04.147Z",
                    "state": "closed"
                },
                {
                    "boardId": 11,
                    "completeDate": "2021-04-07T14:28:50.786Z",
                    "endDate": "2021-04-06T16:33:00.000Z",
                    "goal": "User will be able to create a new promotion simulator and include simulation inputs, select a simulation type. User will be able to view simulation inputs in the \"location\" they selected. User will be able to view predictors from a model on the Predictors tab. User will be able to view simulation inputs on the Spend tab.",
                    "id": 38,
                    "name": "Promo Sim 3",
                    "startDate": "2021-03-24T16:33:56.044Z",
                    "state": "closed"
                },
                {
                    "boardId": 11,
                    "completeDate": "2021-04-21T13:04:46.984Z",
                    "endDate": "2021-04-20T22:26:00.000Z",
                    "goal": "Ability to Publish a Predictive Model, Ability to calculate promo sim metrics, View Summary Results with calced metrics (using the predictor data inserted into backend**)",
                    "id": 39,
                    "name": "Promo Sim 4",
                    "startDate": "2021-04-07T12:12:30.195Z",
                    "state": "closed"
                }
            ],
            "summary": "Simulation Type - Fix \"Remove\" button display & Update \"Volume Components\" to \"Simulation Input\""
        },
        "id": "25391",
        "key": "PA-4699",
        "self": "N/A"
    }

下面是我编写的Python代码,目前只获取列表中[0]customfield_10003的“name”值。我不知道如何循环列表并将每个“名称”值附加到问题列表输出

issue_list = []
for issue in issues:
  if issue['fields']["customfield_10003"]:
    issue_list.append([issue['key'],issue['fields']['summary'],issue["fields"] 
    ["customfield_10003"][0]["name"]])
else:
  issue_list.append([issue['key'],issue['fields']['summary']])enter code here

1条回答
网友
1楼 · 发布于 2024-04-25 07:09:47

如果你能更好地描述你想要的输出,那么就可以提供一个更好的答案

这将遍历json对象并将名称拉入列表

#data = your sample json
data1 = data['fields']['customfield_10003']
names_list = [d['name'] for d in data1]

['Promo Sim 3/23', 'Promo Sim 3', 'Promo Sim 4']

相关问题 更多 >

    热门问题