2014-08-30 42 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