捕获json值并将这些值分配给python列表时出现问题

I have json code from which I need to get requestId and emailTypeDesc that are inside emailList. This action has to be perform for each item on emailRequest list, see below:

json:

{
'readOnly': False,
'senderDetails': {'firstName': 'John', 'lastName': 'Doe', 'emailAddress': 'johndoe@gmail.com', 'emailAddressId': 123456, 'personalId': 123, 'companyName': 'ACME‘},
'clientDetails': {'firstName': 'Jane', 'lastName': 'Doe', 'emailAddress': 'janedoe@gmail.com', 'emailAddressId': 654321, 'personalId': 456, 'companyName': 'Lorem Ipsum‘}},
'notesSection': {},
'emailList': [{'requestId': 12345667, 'emailId': 9876543211, 'emailType': 3, 'emailTypeDesc': 'Email-In', 'emailTitle': 'SampleTitle 1', 'createdDate': '15-May-2020 11:15:52', 'fromMailList': [{'firstName': 'Jane', 'lastName': 'Doe', 'emailAddress': 'janedoe@gmail.com',}]},
              {'requestId': 12345667, 'emailId': 14567775, 'emailType': 3, 'emailTypeDesc': 'Email-Out', 'emailTitle': 'SampleTitle 2', 'createdDate': '16-May-2020 16:15:52', 'fromMailList': [{'firstName': 'Jane', 'lastName': 'Doe', 'emailAddress': 'janedoe@gmail.com',}]},
              {'requestId': 12345667, 'emailId': 12345, 'emailType': 3, 'emailTypeDesc': 'Email-In', 'emailTitle': 'SampleTitle 3', 'createdDate': '17-May-2020 20:15:52', 'fromMailList': [{'firstName': 'Jane', 'lastName': 'Doe', 'emailAddress': 'janedoe@gmail.com',}]   
}

Python代码:

emailRequest=['12345667']
final_list = []

def get_emailRequest_details(emailRequest, tokenId, userId):
headers = {     'accept': 'application/json', 
                'Content-Type': 'application/json', 
                'access-token': tokenId,
                'userID': userId}
endpoint = 'emailRequest/' + x + '/details'
r = requests.get(url = HOSTNAME + endpoint, headers = headers, verify=False)
data = json.loads(r.text)
myDesc = [(data['emailList'][0]['emailTypeDesc'])]
for each_requ in myDesc:
    final_list.append(each_requ)
return final_list

for x in emailRequest:
    get_emailRequest_details(x, tokenId, userId)

However, when running the above code I can't get all values for emailTypeDesc (expected 3 list items, but code returns only latest). Also, not sure how I could get both attributes at the same time (requestId, emailTypeDesc) and add requestId on the front of my final_list.

final_list的当前输出:

['Email-In']

final_list的所需输出:

[12345667, 'Email-In', 'Email-Out', 'Email-In']

Please note that this code will be looping through unknown number of email requests on this list: emailRequest, it can be one request at the time like in this example or many depending on the time this code will run.

因此,对于多个emailRequest,我的final_list输出如下所示:

[12345667, 'Email-In', 'Email-Out', 'Email-In', 987654, 'Email-In', 'Email-Out', 567843, 'Email-In', 'Email-Out', 'Email-In', 'Email-Out', 'Email-In', 'Email-In']

我想我在这里做错了什么:

myDesc = [(data['emailList'][0]['emailTypeDesc'])]
for each_requ in myDesc:
    final_list.append(each_requ)
return final_list

有人可以帮我吗?提前致谢!

评论