我正在开发一个从Crypto Exchange获取信息的程序。我成功地获取了信息并显示了第一条记录,但我无法抓住后续记录。
这是我正在使用的代码,print语句只是为了表明我可以获取1条记录的正确信息,但是在我的整个过程中工作将非常棒。
url = "https://www.cryptopia.co.nz/api/GetCurrencies"
data = json.load(urllib2.urlopen(url))
coin_Id = data["Data"][0]["Id"]
coin_Name = data["Data"][0]["Name"]
coin_Symbol = data["Data"][0]["Symbol"]
coin_Algorithm = data["Data"][0]["Algorithm"]
coin_withdrawfee = data["Data"][0]["WithdrawFee"]
coin_MinBaseTrade = data["Data"][0]["MinBaseTrade"]
coin_IsTipEnabled = data["Data"][0]["IsTipEnabled"]
coin_MinTip = data["Data"][0]["MinTip"]
coin_DepositConfirmations = data["Data"][0]["DepositConfirmations"]
coin_Status = data["Data"][0]["Status"]
coin_StatusMessage = data["Data"][0]["StatusMessage"]
coin_ListingStatus = data["Data"][0]["ListingStatus"]
print "ID: " + str(coin_Id) + "\n" + "Name: " + str(coin_Name) + "Symbol: " + str(coin_Symbol) + "\n" + "Status: " + str(coin_Status) + "\n" + "Status Message: " + str(coin_StatusMessage)
答案 0 :(得分:0)
您可以迭代结果:
<强>实施例强>
import urllib2
import json
url = "https://www.cryptopia.co.nz/api/GetCurrencies"
data = json.load(urllib2.urlopen(url))
print data
for d in data["Data"]:
print d["Id"], d["Name"], d["Symbol"], d["Algorithm"], d["WithdrawFee"]
答案 1 :(得分:0)
我相信你想要做的是循环检索并存储在变量data
中的数据的内容。根据您在问题中提供的代码,您可以执行以下操作:
# Assumes 'url' and 'data' exist per the code sample in your question
for row in data:
coin_Id = row["Id"]
coin_Name = row["Name"]
coin_Symbol = row["Symbol"]
# ...
coin_Status = row["Status"]
coin_StatusMessage = row["StatusMessage"]
coin_ListingStatus = row["ListingStatus"]
# Assumes you grabbed values for all of the keys
print "ID: " + str(coin_Id) + "\n" + "Name: " + str(coin_Name) + "Symbol: " + str(coin_Symbol) + "\n" + "Status: " + str(coin_Status) + "\n" + "Status Message: " + str(coin_StatusMessage)