python json.dumps慢,解析json.dumps-Python

I have a call to an API that returns the following JSON:

{

"trades": [

{

"stopLoss": 154.79,

"takeProfit": 151.79,

"price": 153.784,

"side": "sell",

"trailingStop": 0,

"instrument": "GBP_JPY",

"time": "2016-06-21T18:20:24.000000Z",

"units": 25,

"id": 10297636517,

"trailingAmount": 0

}

]

}

I'm having trouble parsing so that I only return the "id" and the "price". For reasons I don't understand yet, i can add ['trades'] after tradeId=response and it will drill down to just this, as expected. But i cannot get it whittled down to just "id" and "price". How could I go about parsing the data, and is json.dumps the optimal method? Thanks in advance for the help!

{

"stopLoss": 154.79,

"takeProfit": 151.79,

"price": 153.784,

"side": "sell",

"trailingStop": 0,

"instrument": "GBP_JPY",

"time": "2016-06-21T18:20:24.000000Z",

"units": 25,

"id": 10297636517,

"trailingAmount": 0

}

code:

response = oanda.get_trades(account_id)

def transactions():

while response:

tradeId = response

ids = tradeId

line = json.dumps(ids, indent=4)

print line

return

print transactions()

解决方案

You can use json.loads() to parse the json.

import json

response = '{"trades": [{"stopLoss": 154.79, "takeProfit": 151.79, "price": 153.784, "side": "sell", "trailingStop": 0, "instrument": "GBP_JPY", "time": "2016-06-21T18:20:24.000000Z", "units": 25, "id": 10297636517, "trailingAmount": 0}]}'

def transactions():

json_tree = json.loads(response)

trade_list = json_tree['trades']

trade = trade_list[0]

return (trade['id'], trade['price'])

print transactions()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值