Python的JSON GET请求

The_Merendero

我正在使用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无法做到这一点,那么您必须获取所有数据并自行查找。

在示例中,我将一个代码更改为“ 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)        

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章