如何在Python中从JSON中获取字符串?

pgccezyw  于 2023-08-08  发布在  Python
关注(0)|答案(2)|浏览(127)

下面是JSON内容:

{'status': '1', 'message': 'OK',
'result': {'LastBlock': '14934113', 'SafeGasPrice': '119', 'ProposeGasPrice': '119', 'FastGasPrice': '119',
'suggestBaseFee': '118.174590189', 'gasUsedRatio': '0.025821954106323,0.354559890356924,0.173050329701518,0.340210087647076,0.0269005272339711'}}

字符串
如何从'result'中获取字符串?我想得到'SafeGasPrice'

z31licg0

z31licg01#

我首先从result中获取值:gas_unit_price = gas_unit_price.json()["result"]第二次得到我需要的字符串:gas_unit_price = gas_unit_price["FastGasPrice"]

bvn4nwqk

bvn4nwqk2#

import json
data = json.loads('{"status": "1", "message": "OK", "result": {"LastBlock": "14934113", "SafeGasPrice": "119", "ProposeGasPrice": "119", "FastGasPrice": "119", "suggestBaseFee": "118.174590189", "gasUsedRatio": "0.025821954106323,0.354559890356924,0.173050329701518,0.340210087647076,0.0269005272339711"}}')
SafeGasPrice = data["result"]["SafeGasPrice"]
print(SafeGasPrice)

字符串

相关问题