Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-检查变量是否为==到json元素并打印_Python_Json - Fatal编程技术网

Python-检查变量是否为==到json元素并打印

Python-检查变量是否为==到json元素并打印,python,json,Python,Json,我有一个包含数千个学生信息位的json响应。看起来是这样的: { "users": [ { "agents": [ { "href": "private url here", "sourcedId": "g560", "type": "user" } ], "dateLastModified": "

我有一个包含数千个学生信息位的json响应。看起来是这样的:

{
"users": [
    {
        "agents": [
            {
                "href": "private url here",
                "sourcedId": "g560",
                "type": "user"
            }
        ],
        "dateLastModified": "2016-10-24T15:24:00.000Z",
        "demographics": {
            "href": "private url here",
            "sourcedId": "s557",
            "type": "demographics"
        },
        "email": "example@example.com",
        "familyName": "Smith",
        "givenName": "John",
        "identifier": "000000000",
        "metadata": {
            "ext_grade_level": "11"
        },
        "orgs": [
            {
                "href": "private URL here",
                "sourcedId": "000000000000000000000000000000",
                "type": "org"
            },
            {
                "href": "private URL Here",
                "sourcedId": "0000000000000000000000000000000000",
                "type": "org"
            },
            {
                "href": "private url here",
                "sourcedId": "000000000000000000000000000",
                "type": "org"
            }
        ],
        "role": "student",
        "sourcedId": "s557",
        "status": "active",
        "username": "000000000"
    },
    {
然后从“代理”开始重复:[对于下一个学生,大约2500次。我真正想做的是,我有“students\u data”=students\u data.json()

当我运行它时,什么都没有发生。它要求输入,然后就结束了。没有错误,什么都没有。我没有什么要做的,只是它显然是错误的。此外,如果它确实有效,我将如何打印匹配地址的familyName、givenName和标识符

字典中嵌套的列表真的把我搞砸了,因为我知道如何处理列表的唯一方法是使用[0]或[1]等进行索引。但是,在这种情况下,这不是一个选项

另外,我尝试过使用json1['email'],但是我得到了“TypeError:列表索引必须是整数或片,而不是str”,我认为这是因为我跳过了列表


感谢您的帮助!

Json数据可以由相互嵌套的字典和列表组成。在这里,我们通过列表
student_数据['users']
循环查找第一个
student
和匹配的电子邮件

student_email = input("Enter a student email address here:")

for student in students_data['users']:
    if student['email'] == student_email:
        print('found a match')
        print(student)
        break
else:
    print('no match found')

以下是
if-item in json1==stu-email
的工作原理:首先,Python检查
item
是否在
json1
中。无论是不是,结果都是
True
False
,但它们都不等于
stu-email
。您需要一个循环来遍历学生列表并比较
stu-email
to每个学生的电子邮件。这更有意义,是真是假,谢谢。P.s.有人能解释为什么我的问题被否决吗?我想问更好的问题。我会出于安全考虑,在学生和学生的['email'中使用'email'==…。取决于上下文。如果我让每个学生都有一个电子邮件字段,我宁愿提出一个
KeyError
并使程序崩溃,也不愿静静地接受损坏的json数据。错误永远不应该静静地传递。除非明确地沉默感谢洞察力,否则这对我来说非常有效。出于某种原因,我很难思考我在编写处理嵌套数据的代码时,我通常会打开一个python repl。这样,我就可以以交互方式探索数据结构,更好地理解数据类型和数据的形状。
student_email = input("Enter a student email address here:")

for student in students_data['users']:
    if student['email'] == student_email:
        print('found a match')
        print(student)
        break
else:
    print('no match found')