【问题标题】:JSON GET Request with Python使用 Python 的 JSON GET 请求
【发布时间】:2015-11-26 15:34:30
【问题描述】:

我正在使用 Python 编写应用程序。我必须执行 GET 请求才能获取特定信息。我的代码是这样的:

...
conn = httplib.HTTPConnection(self.url)
header = {"Authorization":"Bearer "+self.token}
conn.request("GET","/data",headers=header)
...

我获得的 JSON 与此类似(您可以观察到有 2 个大的不同部分...这只是一个示例,在我的应用程序中这些部分很多)。

[
{
    "createdAt": "2015-11-26T10:06:05.756Z", 
    "date": "2015-10-31T23:00:00.000Z", 
    "files": [], 
    "id": 1, 
    "metadata": {}, 
    "notes": "note impianto 1", 
    "parentSubject": {
        "code": "soggetto1", 
        "createdAt": "2015-11-26T10:05:38.765Z", 
        "id": 1, 
        "metadata": {}, 
        "notes": "note soggetto 1", 
        "personalInfo": 1, 
        "sex": "M", 
        "tags": null, 
        "type": 1, 
        "updatedAt": "2015-11-26T10:05:38.765Z"
    }
}, 
{
    "createdAt": "2015-11-26T10:06:36.684Z", 
    "date": "2015-11-01T23:00:00.000Z", 
    "files": [], 
    "id": 2, 
    "metadata": {}, 
    "notes": "note impianto 2", 
    "parentSubject": {
        "code": "soggetto1", 
        "createdAt": "2015-11-26T10:05:38.765Z", 
        "id": 1, 
        "metadata": {}, 
        "notes": "note soggetto 1", 
        "personalInfo": 1, 
        "sex": "M", 
        "tags": null, 
        "type": 1, 
        "updatedAt": "2015-11-26T10:05:38.765Z"
    }
}
]

例如,如果我提出这个请求:

...
conn.request("GET","/data?id=1",headers=header)
...

我显然只得到了第一部分。问题是我不想获取所有具有id=1 的数据,而是所有具有code=soggetto1 的数据。我该怎么办?

【问题讨论】:

  • 您似乎使用了一些 API。首先检查 API 是否只能向您发送带有"code": "soggetto1" 的数据。如果 API 无法做到这一点,那么您必须获取所有数据并自行查找 "code": "soggetto1"
  • 是的,我正在使用 API,但问题是它没有任何官方文档,因为它实际上正在开发中。
  • 询问开发者他们的 API 是否支持此类请求
  • 我们是否应该猜测非官方的未记录(实际上未命名)API 是如何工作的以及它接受哪些参数?没问题,让我拿我的水晶球,我很快就会回来......更严重的是:你至少尝试过“/data?code=soggetto1”吗?

标签: python json get request


【解决方案1】:

如果 API 不能做到这一点,那么您必须获取所有数据并自行查找。

在示例中,我将一个代码更改为“soggetto2”

data = '''[
{
    "createdAt": "2015-11-26T10:06:05.756Z", 
    "date": "2015-10-31T23:00:00.000Z", 
    "files": [], 
    "id": 1, 
    "metadata": {}, 
    "notes": "note impianto 1", 
    "parentSubject": {
        "code": "soggetto2", 
        "createdAt": "2015-11-26T10:05:38.765Z", 
        "id": 1, 
        "metadata": {}, 
        "notes": "note soggetto 1", 
        "personalInfo": 1, 
        "sex": "M", 
        "tags": null, 
        "type": 1, 
        "updatedAt": "2015-11-26T10:05:38.765Z"
    }
}, 
{
    "createdAt": "2015-11-26T10:06:36.684Z", 
    "date": "2015-11-01T23:00:00.000Z", 
    "files": [], 
    "id": 2, 
    "metadata": {}, 
    "notes": "note impianto 2", 
    "parentSubject": {
        "code": "soggetto2", 
        "createdAt": "2015-11-26T10:05:38.765Z", 
        "id": 1, 
        "metadata": {}, 
        "notes": "note soggetto 1", 
        "personalInfo": 1, 
        "sex": "M", 
        "tags": null, 
        "type": 1, 
        "updatedAt": "2015-11-26T10:05:38.765Z"
    }
}
]'''

#------------------------------------------------------

import json

j = json.loads(data)

results = []

for x in j:
    if x["parentSubject"]["code"] == "soggetto1":
        results.append(x)

print results

你可以用列表理解来做到这一点

results = [ x for x in j if x["parentSubject"]["code"] == "soggetto1" ]

filter()

results = filter(lambda x:x["parentSubject"]["code"] == "soggetto1", j)        

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2015-01-17
    • 2020-06-14
    相关资源
    最近更新 更多