访问JSON API响应中的嵌套数据

2024-04-23 20:38:18 发布

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

我从API请求中获得了以下JSON文件:

 'data': {
 '0x6c3e3fb3e1066132ae54b1049c7f1ffb9c875404': {
     'address': {
         'balance': '39674870110714259',
         'balance_usd': 90.9045327455033,
         'call_count': 10,
         'contract_code_hex': None,
         'contract_created': None,
         'contract_destroyed': None,
         'fees_approximate': '31500129889285741',
         'fees_usd': 72.17424235567398,
         'first_seen_receiving': '2021-03-14 '
         '00:58:46',
         'first_seen_spending': '2021-03-14 '
         '01:03:47',
         'last_seen_receiving': '2021-03-14 '
         '02:12:47',
         'last_seen_spending': '2021-03-14 '
         '02:30:59',
         'nonce': None,
         'received_approximate': '166671175000000000000',
         'received_usd': 320779.4692,
         'receiving_call_count': 4,
         'spending_call_count': 6,
         'spent_approximate': '166600000000000000000',
         'spent_usd': 320642.4844,
         'transaction_count': 10,
         'type': 'account'
     },
     'calls': [{
             'block_id': 12034014,
             'index': '0',
             'recipient': '0x57f41689847f8b0676a0ce8a7c5eb5fb78e79596',
             'sender': '0x6c3e3fb3e1066132ae54b1049c7f1ffb9c875404',
             'time': '2021-03-14 '
             '02:30:59',
             'transaction_hash': '0x777e01251d9c42df4269abcf3c72fd668653f68bf0d4716ef992e57c1bca641c',
             'transferred': True,
             'value': 2.29e+19,
             'value_usd': 44073.9069
         },
         {
             'block_id': 12034002,
             'index': '0',
             'recipient': '0xf582a0db4e787492921d0faee3e19f0e9edee261',
             'sender': '0x6c3e3fb3e1066132ae54b1049c7f1ffb9c875404',
             'time': '2021-03-14 '
             '02:29:30',
             'transaction_hash': '0xc70bdc0efd23bb303d27866806fde689b00fa6e1f8c61850d11e3684bad7d16c',
             'transferred': True,
             'value': 2.5e+19,
             'value_usd': 48115.6189
         }
     ]
 }

}

我想做的是为每次通话访问通话部分的“收件人”部分。 我试过的方法如下:

data=json_response['data']
calls=data['0x6c3e3fb3e1066132ae54b1049c7f1ffb9c875404']['calls']

for i in calls:
    recipient=calls[2]['recipient']
    print(recipient)

然而,这似乎总是在输出中给出相同的接收者

我做错了什么


1条回答
网友
1楼 · 发布于 2024-04-23 20:38:18

在您提供的代码中,您实际上并没有使用迭代器i来访问调用,而是使用calls[2],每次都会得到相同的调用

相反,请使用:

for i in calls:
    recipient=i['recipient']
    print(recipient)

相关问题 更多 >