从请求-响应创建对象列表

2024-04-19 12:31:49 发布

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

我想在通过API调用得到的响应时提取特定对象。 答复=

[
    {'id': '2a15947c-8cdb-4f1d-a1cc-a8d76fd97d61', 'name': 'Human', 'i18nNameKey': 'Blank space Blueprint', 'pluginClone': True
    },
    {'id': '99accff8-9e24-4c76-b21a-f12ef6572369', 'name': 'Robot', 'i18nNameKey': 'Personal space Blueprint', 'pluginClone': True
    },
    {'id': 'bf40b0e5-f151-4df6-b305-4a91b4b7c1da', 'name': 'Dog', 'i18nNameKey': 'Game.blueprints.space.kb.name', 'pluginClone': True
    },
    {'id': '42868b38-b9f8-4540-ba26-0988e8a2e1f7', 'name': 'Bug', 'i18nNameKey': 'Game.blueprints.space.team.name', 'pluginClone': True
    },
    {'id': 'b23eb9fd-0106-452a-8cab-551ce3b45eb0', 'name': 'Cat', 'i18nNameKey': 'Game.blueprints.space.documentation.name', 'pluginClone': True
    },
    {'id': '67668d17-6c08-4c85-a6b6-3c1d6fb23000', 'name': 'Cat', 'i18nNameKey': 'Game.blueprints.space.sp.name', 'pluginClone': True,
    }
]

我需要重新组合id和名称,目前我能够检索它们,但响应不是我想要实现的 我得到的是字符串列表,而不是对象列表,我做错了什么

def listallCategories(self):
self.intentsResponseDict=[]
url = self.url
response = requests.get(url,auth=(self.user,self.password))
json_data = response.json()
#print(json_data)
for resp in json_data:
    self.intentsResponseDict.append(resp['id'])
    self.intentsResponseDict.append(resp['name'])
return self.intentsResponseDict

这就是我得到的

['2a15947c-8cdb-4f1d-a1cc-a8d76fd97d61', 'Human', '99accff8-9e24-4c76-b21a-f12ef6572369', 'Robot', 'bf40b0e5-f151-4df6-b305-4a91b4b7c1da', 'Dog', '42868b38-b9f8-4540-ba26-0988e8a2e1f7', 'Bug', 'b23eb9fd-0106-452a-8cab-551ce3b45eb0', 'Cat', '67668d17-6c08-4c85-a6b6-3c1d6fb23000', 'Bird']

这就是我想要的身份证,姓名

[
    {'2a15947c-8cdb-4f1d-a1cc-a8d76fd97d61', 'Human'
    },
    {'99accff8-9e24-4c76-b21a-f12ef6572369', 'Robot'
    },
    {'bf40b0e5-f151-4df6-b305-4a91b4b7c1da', 'Dog'
    },
    {'42868b38-b9f8-4540-ba26-0988e8a2e1f7', 'Bug'
    },
    {'b23eb9fd-0106-452a-8cab-551ce3b45eb0', 'Cat'
    },
    {'67668d17-6c08-4c85-a6b6-3c1d6fb23000', 'Bird'
    }
]

Tags: nameselfidgamejsontruespacecat
3条回答

您正在将ID和名称添加到列表的末尾

for resp in json_data:
    self.intentsResponseDict.append(resp['id']) # here is your issue
    self.intentsResponseDict.append(resp['name'])  # and here
return self.intentsResponseDict

如果希望这些值保持在一起,则需要将它们一起添加到列表中。e、 g

for resp in json_data:
    self.intentsResponseDict.append([resp['id'], resp['name']]) # to create list of lists
    # OR
    self.intentsResponseDict.append({resp['id'], resp['name']}) # to create list of dicts
return self.intentsResponseDict

{}可能类似于:

[
  {
    "id": "2a15947c-8cdb-4f1d-a1cc-a8d76fd97d61",
    "name": "Human",
    ... more fields ...
  },
  {
    "id": "99accff8-9e24-4c76-b21a-f12ef6572369",
    "name": "Robot",
    ... more fields ...
  },
... more objects ...
]

您想要的数据格式不是有效的JSON,更好的JSON模式可能是:

[
  {
    "id": "2a15947c-8cdb-4f1d-a1cc-a8d76fd97d61",
    "name": "Human",
  },
  {
    "id": "99accff8-9e24-4c76-b21a-f12ef6572369",
    "name": "Robot",
  },
... more objects ...
]

因此,您只是在响应中选择了idname

for resp in json_data:
    self.intentsResponseDict.append({
        "id": resp["id"],
        "name": resp["name"]
    })

您显示的是一个集合列表。只需构建它:

...
for resp in json_data:
    self.intentsResponseDict.append({resp['id'], resp['name']})
...

相关问题 更多 >