如何选择带有id的行json

时间:2015-02-18 08:45:14

标签: python json

我有这个

{"id":"141","bid":4.57000002,"ask":4.89999798},{"id":"345","bid":79933.93185001,"ask":92999.99999999}

我正在使用python 如何才能获得只有出价的出价或要价?

1 个答案:

答案 0 :(得分:2)

假设您的JSON数据中有一个词典列表:

def get_by_id(lst, id):
    for d in lst:
        if d.get('id') == str(id):
            return d
    return None

>>> import json

>>> data = '[{"id": "141", "bid": 4.57000002, "ask": 4.89999798}, {"id": "345", "bid": 79933.93185001, "ask": 92999.99999999}]'
>>> lst = json.loads(data)
>>> d = get_by_id(lst, 345)

>>> d['ask']
92999.99999999

>>> d['bid']
79933.93185001