【问题标题】:JSON API get requests return only one particular fieldJSON API 获取请求仅返回一个特定字段
【发布时间】:2017-05-12 10:06:35
【问题描述】:

我很困惑。我从其他相关问题中尝试过的东西都没有奏效,我似乎无法通过谷歌搜索找到它。

我想用 Python 过滤一个 json api 响应,而不仅仅是对我返回的数据源进行排序——这对我来说并不是什么好事。我还想明确选择一个字段,而不是删除所有其他字段,因为有时字段比其他时候多。它必须通过请求的结构来完成,而不是事后修改任何东西。

我使用以下代码从 matchbook api 获取运动列表,但我将如何仅显示这些运动的名称,过滤掉 id 和类型?

r7 = s.get('https://api.matchbook.com/edge/rest/lookups/sports')

r8 = r7.json()
pprint.pprint (r8)

这是我回来的:

{'offset': 0,
 'per-page': 20,
 'sports': [{'id': 112, 'name': 'Australian Rules', 'type': 'SPORT'},
            {'id': 13, 'name': 'Auto Racing', 'type': 'SPORT'},
            {'id': 3, 'name': 'Baseball', 'type': 'SPORT'},
            {'id': 4, 'name': 'Basketball', 'type': 'SPORT'},
            {'id': 14, 'name': 'Boxing', 'type': 'SPORT'},
            {'id': 110, 'name': 'Cricket', 'type': 'SPORT'},
            {'id': 11, 'name': 'Current Events', 'type': 'SPORT'},
            {'id': 115, 'name': 'Cycling', 'type': 'SPORT'},
            {'id': 116, 'name': 'Darts', 'type': 'SPORT'},
            {'id': 117, 'name': 'Gaelic Football', 'type': 'SPORT'},
            {'id': 8, 'name': 'Golf', 'type': 'SPORT'},
            {'id': 241798357140019,
             'name': 'Greyhound Racing',
             'type': 'SPORT'},
            {'id': 24735152712200, 'name': 'Horse Racing', 'type': 'SPORT'},
            {'id': 222109340250019,
             'name': 'Horse Racing (Ante Post)',
             'type': 'SPORT'},
            {'id': 231138347942400,
             'name': 'Horse Racing Beta',
             'type': 'SPORT'},
            {'id': 118, 'name': 'Hurling', 'type': 'SPORT'},
            {'id': 6, 'name': 'Ice Hockey', 'type': 'SPORT'},
            {'id': 126, 'name': 'MMA', 'type': 'SPORT'},
            {'id': 5, 'name': 'NCAA Basketball', 'type': 'SPORT'},
            {'id': 2, 'name': 'NCAA Football', 'type': 'SPORT'}],
 'total': 28}
401
[Finished in 5.3s]

如何去掉“id”和“type”字段?

这是一大组json数据的摘录:

    {'events': [{'allow-live-betting': True,
             'category-id': [9,
                             297063445660036,
                             410444698700010,
                             410468520880009,
                             477274740480010,
                             486941505790009],
             'id': 488243069570009,
             'in-running-flag': True,
             'markets': [{'allow-live-betting': True,
                          'back-overround': 102.04249,
                          'event-id': 488243069570009,
                          'id': 488243069830010,
                          'in-running-flag': True,
                          'lay-overround': 97.95751,
                          'live': True,
                          'market-type': 'money_line',
                          'name': 'Moneyline',
                          'runners': [{'event-id': 488243069570009,
                                       'event-participant-id': 488243069720009,
                                       'id': 488243069860009,
                                       'market-id': 488243069830010,
                                       'name': 'G Mager',
                                       'prices': [{'available-amount': 48.42639,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 1.53191,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 1.53191,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'back'},
                                                  {'available-amount': 473.49579,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 1.51546,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 1.51546,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'back'},
                                                  {'available-amount': 96.87754,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 1.40322,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 1.40322,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'back'},
                                                  {'available-amount': 710.74,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 1.5814,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 1.5814,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'lay'},
                                                  {'available-amount': 163.74192,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 1.76336,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 1.76336,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'lay'},
                                                  {'available-amount': 151.55072,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 1.9434,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 1.9434,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'lay'}],
                                       'status': 'open',
                                       'volume': 6955.72965},
                                      {'event-id': 488243069570009,
                                       'event-participant-id': 488243069660010,
                                       'id': 488243069890010,
                                       'market-id': 488243069830010,
                                       'name': 'A Bedene',
                                       'prices': [{'available-amount': 413.22093,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 2.72,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 2.72,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'back'},
                                                  {'available-amount': 124.99383,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 2.31,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 2.31,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'back'},
                                                  {'available-amount': 142.97238,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 2.06,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 2.06,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'back'},
                                                  {'available-amount': 25.75872,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 2.88,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 2.88,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'lay'},
                                                  {'available-amount': 244.07,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 2.94,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 2.94,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'lay'},
                                                  {'available-amount': 39.06352,
                                                   'currency': 'GBP',
                                                   'decimal-odds': 3.48,
                                                   'exchange-type': 'back-lay',
                                                   'odds': 3.48,
                                                   'odds-type': 'DECIMAL',
                                                   'side': 'lay'}],
                                       'status': 'open',
                                       'volume': 16033.12548}],
                          'start': '2017-05-15T11:05:00.000Z',
                          'status': 'open',
                          'type': 'binary',
                          'volume': 22988.85514}],
             'meta-tags': [{'id': 9,
                            'name': 'Tennis',
                            'type': 'SPORT',
                            'url-name': 'tennis'},
                           {'id': 297063445660036,
                            'name': 'R1',
                            'type': 'DATE',
                            'url-name': 'r1'},
                           {'id': 410444698700010,
                            'name': 'Italy',
                            'type': 'COUNTRY',
                            'url-name': 'italy'},
                           {'id': 410468520880009,
                            'name': 'Live Betting',
                            'type': 'OTHER',
                            'url-name': 'live-betting'},
                           {'id': 477274740480010,
                            'name': 'May 15th 2017',
                            'type': 'DATE',
                            'url-name': 'may-15th-2017'},
                           {'id': 486941505790009,
                            'name': 'ATP Rome',
                            'type': 'COMPETITION',
                            'url-name': 'ATP-Rome'}],
             'name': 'G Mager vs A Bedene',
             'sport-id': 9,
             'start': '2017-05-15T11:05:00.000Z',
             'status': 'open',
             'volume': 23098.72333},
 'offset': 0,
 'per-page': 20,
 'total': 223}

我想在这个数据中找到 event-id 并打印 "'event-id': 488243069570009"

我设法做到了:

r11 = s.get('https://api.matchbook.com/edge/rest/events')

r12 = r11.json()

for each in r12['events'][0]['markets']:
    print(each['event-id'])

但它只在完整的 json 数据集上打印第一个 event-id

【问题讨论】:

  • [ s['name'] for s in r8['sports'] ]
  • 那么,API 是否允许您提出这样的请求?如果没有,那你就倒霉了:你只能得到 API 提供的东西,你不能让它做它不打算做的事情。这意味着您需要发出一个请求,以最少量的“绒毛”获取您想要的数据,并自己过滤掉这些绒毛。

标签: python json rest


【解决方案1】:

您可以简单地使用速记创建一个新数组

data = {"offset":0,"per-page":20,"sports":[{"id":112,"name":"Australian Rules","type":"SPORT"},{"id":13,"name":"Auto Racing","type":"SPORT"},{"id":3,"name":"Baseball","type":"SPORT"},{"id":4,"name":"Basketball","type":"SPORT"},{"id":14,"name":"Boxing","type":"SPORT"},{"id":110,"name":"Cricket","type":"SPORT"},{"id":11,"name":"Current Events","type":"SPORT"},{"id":115,"name":"Cycling","type":"SPORT"},{"id":116,"name":"Darts","type":"SPORT"},{"id":117,"name":"Gaelic Football","type":"SPORT"},{"id":8,"name":"Golf","type":"SPORT"},{"id":241798357140019,"name":"Greyhound Racing","type":"SPORT"},{"id":24735152712200,"name":"Horse Racing","type":"SPORT"},{"id":222109340250019,"name":"Horse Racing (Ante Post)","type":"SPORT"},{"id":231138347942400,"name":"Horse Racing Beta","type":"SPORT"},{"id":118,"name":"Hurling","type":"SPORT"},{"id":6,"name":"Ice Hockey","type":"SPORT"},{"id":126,"name":"MMA","type":"SPORT"},{"id":5,"name":"NCAA Basketball","type":"SPORT"},{"id":2,"name":"NCAA Football","type":"SPORT"}],"total":28}
# print(data['sports'])
# for element in data['sports']: 
#     del element['id'] 
#     del element['type']
# print(data['sports'])
new_arr =  [_['name'] for _ in data['sports']];
print(new_arr)

更新 检查online demo

【讨论】:

  • 是的,但这是在我取回数据后对数据进行排序。我只想请求某些字段。正如我解释的那样,否则对我来说没什么好处。
  • @Davtho1983 我已经添加了演示。它没有按照演示中的数据排序。请检查并告诉我
  • 不行还是不行 - 我只想选择名称字段,而不是删除所有其他字段。有时字段比其他时候多,捕获所有错误会很痛苦。
  • 好的,太好了 - 感谢您的耐心等待 - 我对 python 和 API 都是新手。还有一件事-我无法将其应用于进一步嵌套在 Json 树中的东西?告诉它查看特定位置的语法是什么?
  • 说我有 json 开头:{ "id": 395729780570010, "name": "CSKA Moscow vs Bayer 04 Leverkusen", "sport-id": 15, "start": "2016-11 -22T17:00:00.000Z", "in-running-flag": true, "allow-live-betting": true, "category-id": [15, 394655963100010, 395729779790010, 395729779910010], "status": " open", "volume": 0, "markets": [ { "event-id": 395729780570010, "id": 395729860260010, "name": "Match Odds", "runners": [ {
【解决方案2】:

You can't do what you're asking the way you're asking。火柴盒 API 不能那样工作。

你可以做一些傻事,比如在你和他们为你过滤的 API 之间添加一个代理服务器,所以 不是做过滤的人,它是代理,但是这只是在玩语义。

Agam Banga's example完全按照你所说的去做:

我如何只显示这些运动的名称,过滤掉 id 和类型?

这是一个完整的示例,您可以直接复制、粘贴和运行,它会打印出火柴盒支持的所有运动:

import requests
r = requests.get('https://api.matchbook.com/edge/rest/lookups/sports')                                                                                                              
names = [_['name'] for _ in r.json()['sports']]                                                                                                                                     

for name in names:                                                                                                                                                                  
    print('Sport Name:', name)

我运行了它,它的输出如下:

Sport Name: Australian Rules
Sport Name: Auto Racing
Sport Name: Baseball
Sport Name: Basketball
Sport Name: Boxing
Sport Name: Cricket
Sport Name: Current Events
Sport Name: Cycling
Sport Name: Darts
Sport Name: Gaelic Football
Sport Name: Golf
Sport Name: Greyhound Racing
Sport Name: Horse Racing
Sport Name: Horse Racing (Ante Post)
Sport Name: Horse Racing Beta
Sport Name: Hurling
Sport Name: Ice Hockey
Sport Name: MMA
Sport Name: NCAA Basketball
Sport Name: NCAA Football

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 2020-06-13
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多