2014-08-30 49 views
0

我有一个问题,我将从一个API获取信息和信息会像被退回:如何删除列表参数但不是内容?

[{"market_id":"16","coin":"Dogecoin","code":"DOGE","exchange":"BTC","last_price":"0.00000025","yesterday_price":"0.00000025","change":"0.00","24hhigh":"0.00000026","24hlow":"0.00000025","24hvol":"6.732","top_bid":"0.00000025","top_ask":"0.00000026"}] 

这使得它真的很难称之为例如价格。所以我想知道是否有办法摆脱名单[ ]并仍然保留这些数据?预先感谢一堆!

+2

对我来说看起来像[JSON](http://json.org)。你应该看看这个JSON解析器。 – Strikeskids 2014-08-30 02:42:28

+1

我查看了Ctrl + F的JSON网站,发现它是JSON。感谢您的认识! – user3818089 2014-08-30 02:44:41

回答

3

当然 - 我假设你已经解析的JSON - 因为你只需要保持第一元素(因为显然只有一个元素),你可以这样做:

data = data[0] 
2
import json 
data = '[{"market_id":"16","coin":"Dogecoin","code":"DOGE","exchange":"BTC","last_price":"0.00000025","yesterday_price":"0.00000025","change":"0.00","24hhigh":"0.00000026","24hlow":"0.00000025","24hvol":"6.732","top_bid":"0.00000025","top_ask":"0.00000026"}]' 
# since the list only contains one element (the dict), we'll access it with [0] 
dictionary = json.loads(data)[0] 
print dictionary["last_price"] 
+0

我得到'TypeError:字符串索引必须是整数,而不是str'。谢谢! – user3818089 2014-08-30 02:50:13

+0

@ user3818089是的,那是因为你的例子是一个列表,而不是一个str。但我认为你忘记了你的问题细节中的引号,它实际上是一个JSON str(不是一个列表)。看看我的编辑。 – sgarza62 2014-08-30 02:56:51