简体   繁体   English

从JSON响应中提取

[英]Extracting from JSON response

I have a Python script that fetches a JSON response: 我有一个获取JSON响应的Python脚本:

import requests
import json

bitcoin_api_url = 'https://example.com'
response = requests.get(bitcoin_api_url)
response_json = response.json()
print(response_json)

Result: 结果:

[{'id': 'bitcoin', 'name': 'Bitcoin', 'symbol': 'BTC', 'rank': '1', 'price_usd': '4068.19769898', 'price_btc': '1.0', '24h_volume_usd': '9967726932.86', 'market_cap_usd': '71664809254.0', 'available_supply': '17615862.0', 'total_supply': '17615862.0', 'max_supply': '21000000.0', 'percent_change_1h': '-0.42', 'percent_change_24h': '-0.01', 'percent_change_7d': '0.95', 'last_updated': '1553837126'}]

How can I extract values from this using Python? 如何使用Python从中提取值? For example, I'd like to ask it for 'id' and get 'bitcoin' as a response. 例如,我想要求它提供'id'并获得'bitcoin'作为响应。

simple code as per your requirement 根据您的要求编写简单的代码

   for i in d:
      print(i['id'])

It's returning a list containing a dictionary. 它正在返回一个包含字典的列表。 Pull the dictionary out of the list, then you can grab individual values from the dictionary. 将字典从列表中拉出,然后可以从字典中获取单个值。

response_json = response.json()[0]
id = response_json['id']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM