簡體   English   中英

從JSON數據API獲取特定值

[英]Grabbing Specific Value from JSON Data API

我完全知道此網站上還有其他主題,但是解決方案對我不起作用。

我正在嘗試從以下API地址獲取“最后一個”值: https//cryptohub.online/api/market/ticker/PLSR/

在Python中,我嘗試了許多不同的腳本,並嘗試了自己的努力,但是盡管API中包含“ last”,但我總是最終得到“ KeyError”。 誰能幫我?

您的響應數據具有以下結構:

$ curl  https://cryptohub.online/api/market/ticker/PLSR/ | json_pp
{
   "BTC_PLSR" : {
      "baseVolume" : 0.00772783,
      "lowestAsk" : 0.00019999,
      "percentChange" : -0.0703703704,
      "quoteVolume" : 83.77319071,
      "last" : 0.000251,
      "id" : 78,
      "highestBid" : 5e-05,
      "high24hr" : 0.000251,
      "low24hr" : 1.353e-05,
      "isFrozen" : "0"
   }
}

即字典里面的字典,所以要提取值,您需要:

data = response.json()["BTC_PLSR"]
visitors = data["id"]

評論更新:

我不確定我是否了解您,我做了一個簡單的測試,它工作正常:

$ python3 << EOF
> import requests
> url = 'https://cryptohub.online/api/market/ticker/PLSR/'
> response = requests.get(url)
> data = response.json()['BTC_PLSR']
> print('ID ==> ', data['id'])
> EOF
ID ==>  78

如您所見,行print('ID ==> ', data['id'])返回輸出ID ==> 78

測試源代碼:

import requests
url = 'https://cryptohub.online/api/market/ticker/PLSR/'
response = requests.get(url)
data = response.json()['BTC_PLSR']
print('ID ==> ', data['id'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM