如何在python中访问JSON文件中的数据?我想访问电子邮件数据

如何在python中访问JSON文件中的数据?我想访问电子邮件数据,python,json,Python,Json,我已经创建了这样的JSON文件,我想访问电子邮件 这是JSON文件 { "customers": { "OscarLang": { "email": "gmail@gmail.com", "events": [] }, "foretz-abdo": { "em

我已经创建了这样的JSON文件,我想访问电子邮件 这是JSON文件

{
            "customers": {
                "OscarLang": {
                    "email": "gmail@gmail.com",
                    "events": []
                },
                "foretz-abdo": {
                    "email": "hotmail@hotmail.com",
                    "events": []
                },
                "testuser": {
                    "email": "test@hotmail.com",
                    "events": []
                },
                "AAS": {
                    "email": "osdaadawdaw@asdad.com",
                    "events": []
                }
            }
        }
这就是我所尝试的

 sources = ["customers.json"]
    for source in sources:
        data = util.get_data_item(source)
        if data is None or len(data) == 0:
            continue
        register = json.loads(data)
        for json_obj in register['customers']:
            print(json_obj)
            try:
                emailaddress = json_obj['email']
            except Exception as e:
                emailaddress = None

            print(emailaddress)

当我打印emailaddress时,我没有收到任何邮件

欢迎使用堆栈溢出

看看这样的东西是否适合你:

项目树:

project
  |__customers.json
  |
  |__customers.py
customers.json文件

{
  "customers": {
    "OscarLang": {
      "email": "gmail@gmail.com",
      "events": []
    },
    "foretz-abdo": {
      "email": "hotmail@hotmail.com",
      "events": []
    },
    "testuser": {
      "email": "test@hotmail.com",
      "events": []
    },
    "AAS": {
      "email": "osdaadawdaw@asdad.com",
      "events": []
    }
  }
}
# Import Python's json package
import json

# Specify the json file to work with
sources = 'customers.json'

# Open the json file
with open(sources) as json_file:
    # Extract the data from the json file
    data = json.load(json_file)

    # Iterate over each customer in the file
    for customer in data['customers']:
        # Print each customer's e-mail address
        try:
            email = data['customers'][customer]['email']
            print(email)
        except KeyError as err:
            print(f"Email not found for {customer}")
customers.py文件

{
  "customers": {
    "OscarLang": {
      "email": "gmail@gmail.com",
      "events": []
    },
    "foretz-abdo": {
      "email": "hotmail@hotmail.com",
      "events": []
    },
    "testuser": {
      "email": "test@hotmail.com",
      "events": []
    },
    "AAS": {
      "email": "osdaadawdaw@asdad.com",
      "events": []
    }
  }
}
# Import Python's json package
import json

# Specify the json file to work with
sources = 'customers.json'

# Open the json file
with open(sources) as json_file:
    # Extract the data from the json file
    data = json.load(json_file)

    # Iterate over each customer in the file
    for customer in data['customers']:
        # Print each customer's e-mail address
        try:
            email = data['customers'][customer]['email']
            print(email)
        except KeyError as err:
            print(f"Email not found for {customer}")

嗨,Abdo,在写下一个问题之前,请仔细阅读!快乐编码:)这能回答你的问题吗?我尝试了这个,但是我得到了这个错误类型错误:列表索引必须是整数你有错误的完整堆栈跟踪吗?我更新了我的答案,以显示我用来运行的json文件和格式