Json 如何迭代变量以传递到http头参数中,以便进一步迭代

Json 如何迭代变量以传递到http头参数中,以便进一步迭代,json,python-3.x,Json,Python 3.x,我有2个json api,我正在请求;搜索和扩展配置文件 第一个给了我一些个人资料的搜索结果。对于找到的每个配置文件,搜索结果都有一个“memberid”编号ps['id'] 我想将这些成员ID传递并迭代到下一个json api,以获得每个成员的扩展配置文件信息。memberid必须传递到配置文件参数中。现在,只传递和存储了1个memberid,因此我只获得了第一个扩展配置文件,而不是全部来自搜索 我的代码如下: # Search for profiles search_response = r

我有2个json api,我正在请求;搜索和扩展配置文件

第一个给了我一些个人资料的搜索结果。对于找到的每个配置文件,搜索结果都有一个“memberid”编号
ps['id']

我想将这些成员ID传递并迭代到下一个json api,以获得每个成员的扩展配置文件信息。memberid必须传递到配置文件参数中。现在,只传递和存储了1个memberid,因此我只获得了第一个扩展配置文件,而不是全部来自搜索

我的代码如下:

# Search for profiles
search_response = requests.post('https://api_search_for_profiles', headers=search_headers, data=search_params)
search_json = json.dumps(search_response.json(), indent=2)
search_data = json.loads(search_json)

memberid = []
for ps in (search_data['data']['content']):
    memberid = str(ps['id']) # These memberid's I want to pass all found to the profile_params.
    print('UserID: ' + str(ps['roomNo']))
    print('MemberID: ' + str(ps['id']))
    print('Username: ' + ps['nickName'])

# Extended profile info
profile_headers = {
    'x-auth-token': f'{token}',
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'okhttp/3.11.0',
}

profile_params = {
    'id': '',
    'token': f'{token}',
    'memberId': f'{memberid}', # where I want each memberid from the search to go
    'roomNo': ''
}

profile_response = requests.post('https://api_extended_profile_information', headers=profile_headers, data=profile_params)
profile_json = json.dumps(profile_response.json(), indent=2)
profile_data = json.loads(profile_json)
pfd = profile_data['data'] # main data

userid = str(pfd['roomNo'])
username = pfd['nickName']
gender = str(pfd['gender'])
level = str(pfd['memberLevel'])

# Here I will iterate through each profiles with the corresponding memberid and print.
搜索的json输出如下所示,代码段:

{
  "code": 0,
  "data": {
    "content": [
      {
        "id": 1359924,
        "memberLevel": 1,
        "nickName": "akuntesting dgt",
        "roomNo": 1820031
      },
      {
        "id": 2607179,                
        "memberLevel": 1,
        "nickName": "testingsyth",        
        "roomNo": 3299390        
      }, 
      # ... and so on

假设post请求只接受一个memberid,下面是代码的简化版本,旨在处理多个memberid的问题。从这里开始:

memberids = []
for ps in (search_data['data']['content']):
    memberid = str(ps['id'])
    memberids.append(memberid)

for memberid in memberids:
    profile_params = {'memberId': memberid}
    profile_response = requests.post('https://api_extended_profile_information', headers=profile_headers, data=profile_params)
    #the rest of your code goes here inside the loop

试试看,让我知道它是否有效。

扩展配置文件api是否允许使用多个memberid的post请求,还是要求您对每个memberid单独请求?我认为您需要验证它;一对一比较简单。我不知道如何验证它。我不知道如何发布多个memberid请求。现在只花了一次,我的工作很有魅力。在我以前找不到关于如何做到这一点的逻辑的地方,当我知道呵呵的时候,这是非常明显和简单的。非常感谢。