[
{
"confirmed": false,
"client": {
"Outcome": {
"ResponseId": "64352036ead00dc1fd8d",
"Options": [
{
"optionState": "AUTHORISED",
"Endorsements": [
{
"PointId": "7e8fb6e0-0435-45c3-b2a5-82cb2074bf63",
"LifetimeIds": ["00002"],
"Code": "001",
"Name": "Name of the provider",
"Wording": "Wording goes here",
"Variables": [{ "name": "${value}", "value": "450" }]
}
],
"subscriptions": [
{
"totalAmount": { "amount": "450.0", "currencyCode": "EUR" },
"tax": { "amount": "0.0", "currencyCode": "EUR" },
"subscription": { "amount": "450.0", "currencyCode": "EUR" },
"code": "Movie",
"message": "You have access to all movies in the library"
},
{
"totalAmount": { "amount": "25.0", "currencyCode": "EUR" },
"tax": { "amount": "0.0", "currencyCode": "EUR" },
"subscription": { "amount": "25.0", "currencyCode": "EUR" },
"code": "Library",
"message": "You are able to borrow books"
}
]
}
]
}
},
"startDate": "17-04-2023",
"name": "Joe Blogg",
"email": "joe@blogg.com",
"phoneNumber": "01234567890"
}
]
我想知道是否有人可以帮助我,我试图循环通过'subscribes'元素并只打印出['tax']['amout']和['totalAmount']['amount'] -将它们添加到字典中。
list indices must be integers or slices, not str
所以我希望有一个新的字典,有这样的格式:
"subscriptions": [
{
"totalAmount": { "amount": "450.0" },
"tax": { "amount": "0.0"}
},
{
"totalAmount": { "amount": "25.0"},
"tax": { "amount": "0.0"}
}
]
new_dict = {}
for i in data['client']['Outcome']['Options']['subscriptions']:
new_dict.update({'totalAmount': i['totalAmount']['amount']})
new_dict.update({'tax': i['tax']['amount']})
代码是我试图用来解析它的,但是它给了我错误-所以我不能用我感兴趣的值构建一个新的字典。
任何帮助将不胜感激。
1条答案
按热度按时间nwnhqdif1#
如果每个列表只有一条记录,则可以用途:
输出: