简体   繁体   English

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

[英]Return json object(s) in list by key: value in Python

I have this list of json objects, and I'd like to create a method that returns all commit (or project) entries where project_id : 1 , for example. 我有此json对象的列表,并且我想创建一个返回所有commit(或project)条目的方法,例如project_id : 1 Is this possible? 这可能吗?

I currently have all_commits = all_data[0]['commits'] , which represents all of the commit objects, and a method def findAll(self):return all_commits which returns all of them, but I cant figure out how to return some of the records. 我目前有all_commits = all_data[0]['commits'] ,它表示所有提交对象,以及一个方法def findAll(self):return all_commits ,它返回所有提交对象,但是我不知道如何返回一些记录。 I can return the value of a record via: 我可以通过以下方式返回记录的

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

But how can I return the whole object? 但是如何返回整个对象?


     [
            {
                "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"
                    }
                ]
            }
        ]

用列表推导(或到达那里的一个genex)进行过滤。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM