我有这段代码
import json,urllib2
def Trade(item):
url = json.loads(urllib2.urlopen(urllib2.Request('https://api.warframe.market/v1/items/'+item+'/orders')).read())
primo = url["payload"]
secondo = primo["orders"]
prezzi = []
for tata in secondo:
price = tata["platinum"]
prezzi.append(price)
print sorted(prezzi,key=int)
我得到一个有序的DESC列表,但我需要通过“状态”过滤它,在url [“payload”] [“orders”] [“user”] [“status”]。
我对json解析有点新,我需要用整数定义子级别,但是我需要解析整个json以按用户的在线状态命令“price”DESC。
我该怎么办?
以下是我的JSON数据示例:https://api.warframe.market/v1/items/primed_chamber/orders
答案 0 :(得分:0)
根据你的问题(不太清楚),但据我所知,你需要"价格"在线用户列表,"价格"在你的json数据中表示铂金值。所以要这样做。
试试这个:
import json
filedata = open('jsondata.txt', 'r')
jsondata = json.load(filedata)
ingame_users_platinum = []
offline_users_platinum = []
for i in jsondata['payload']['orders']:
if i['user']['status'] == 'offline':
offline_users_platinum.append(i['platinum'])
elif i['user']['status'] == 'ingame':
ingame_users_platinum.append(i['platinum'])
print("Price/Platinum of ingame users: \n", ingame_users_platinum)
print("Price/Platinum of offline users: \n", offline_users_platinum)
这将为您提供如下输出:
Price/Platinum of ingame users:
[35000, 70000, 2000, 69, 10, 75000, 500, 1000, 100, 500]
Price/Platinum of offline users:
[30000, 28282, 10000, 3200, 1, 2000, 25, 666, 55000, 10000, 2000, 30, 25000, 4200, 10, 20000, 100, 1000, 45000, 20000, 1000, 4500, 300, 20000, 20000, 20000, 50000, 66666, 6666, 200, 200, 5000, 50000, 11000, 8, 100, 500, 50, 4500, 66000, 69000, 5, 20, 150, 400, 42, 5, 20000, 25000, 36000, 69, 10, 1000, 1, 450, 1, 40000, 1000, 120, 68000, 100, 1, 20000, 322, 1, 1, 60000, 50000, 53000, 5, 5000, 1, 1000, 1, 1, 100, 8000, 10000, 40000, 5000, 100, 1, 1, 200, 20, 1, 500, 46, 500, 1, 40000, 20, 133337, 911, 188888, 10000, 1, 1, 1, 1, 1, 30000, 200, 40000, 100000, 90000, 199999, 1, 10, 1, 150]
要在desc中对这些列表进行排序: 这样做:
ingame_users_platinum.sort(key=int, reverse=True) # For sorted in desc
offline_users_platinum.sort(key=int, reverse=True) # For sorted in desc
然后再次打印它们以获得输出。 赞这个:
Price/Platinum of ingame users:
[75000, 70000, 35000, 2000, 1000, 500, 500, 100, 69, 10]
Price/Platinum of offline users:
[199999, 188888, 133337, 100000, 90000, 69000, 68000, 66666, 66000, 60000, 55000, 53000, 50000, 50000, 50000, 45000, 40000, 40000, 40000, 40000, 36000, 30000, 30000, 28282, 25000, 25000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 11000, 10000, 10000, 10000, 10000, 8000, 6666, 5000, 5000, 5000, 4500, 4500, 4200, 3200, 2000, 2000, 1000, 1000, 1000, 1000, 1000, 911, 666, 500, 500, 500, 450, 400, 322, 300, 200, 200, 200, 200, 150, 150, 120, 100, 100, 100, 100, 100, 69, 50, 46, 42, 30, 25, 20, 20, 20, 10, 10, 10, 8, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
希望这会对你有所帮助! :)