按键返回列表中的json对象:Python中的值

时间:2015-03-06 18:29:47

标签: python json

我有这个json对象列表,我想创建一个返回所有提交(或项目)条目的方法,例如project_id : 1。这可能吗?

我目前有all_commits = all_data[0]['commits']代表所有提交对象,方法def findAll(self):return all_commits返回所有提交对象,但我无法弄清楚如何返回一些的记录。我可以通过以下方式返回记录的

  def findOne(self, index, key):
    return all_commits[index][key]

但我怎样才能归还整个物体?


     [
            {
                "commits": [
                    {
                        "project_id": "1",
                        "commit_title": "commit 1",
                        "date": "date 1",
                        "markdown": "markdown 1"
                    },
                    {
                        "project_id": "1",
                        "commit_title": "commit 2",
                        "date": "date 2",
                        "markdown": "markdown 2"
                    },
                    {
                        "project_id": "1",
                        "commit_title": "commit 3",
                        "date": "date 3",
                        "markdown": "markdown 3"
                    },
                    {
                        "project_id": "1",
                        "commit_title": "commit 4",
                        "date": "date 4",
                        "markdown": "markdown 4"
                    },
                    {
                        "project_id": "2",
                        "commit_title": "commit 5",
                        "date": "date 5",
                        "markdown": "markdown 5"
                    },
                    {
                        "project_id": "2",
                        "commit_title": "commit 6",
                        "date": "date 6",
                        "markdown": "markdown 6"
                    }
                ]
            },
            {
                "projects": [
                    {
                        "id": 1,
                        "project_name": "GreenGlass for Groups",
                        "description": "Support group projects for retention agreements"
                    },
                    {
                        "id": 2,
                        "project_name": "Zumo Redesign",
                        "description": "New eda theme-based design"
                    }
                ]
            }
        ]

1 个答案:

答案 0 :(得分:1)

使用列表推导过滤(或者到达那里时使用genex)。

[commit for commit in all_commits if commit['project_id'] == '1']