如何在复杂的嵌套JSON中解决关键错误

发布于 2025-02-09 18:35:24 字数 1888 浏览 1 评论 0原文

我不断收到关键错误“笔记”。笔记处于嵌套响应中。我该如何解决?我包括了JSON的样本。 通过请求添加了追溯。

    Traceback (most recent call last):
  File "/Users/xxxxxx/Documents/code/trade show w notes", line 16, in <module>
    target = "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (contact["company_name"], contact["location"], contact["summary"], contact["job_title"], contact["name"], contact["job_industry"], contact["email"], contact["first_name"], contact["last_name"], contact["notes"])
KeyError: 'notes'

\ \

"data": [
    {
        "team_id": 53806,
        "name": "Nicholas Bancroft Cooke",
        "first_name": "Nicholas",

        ],
        "email": null,
        "metadata": null,
        "qualification": [
            {
                "qualification_id": 17573056,
                "qualification": "connected",

                "notes": null,

\

     page = 1
     url = "https://teams-api.grip.events/1/team/53806/event/123721/member/236388/contact/inbound_lead/reviewed?page=1"
     headers = {
         'authorization': 'Bearer eaf3bd4b-6861-4ca2-a86e-3a96c73deac0',
}
data = ["company_name", "job_title", "name",  "job_industry", "summary", "notes", "location", "first_name", "last_name", "email"]
with open("list31.txt", "w", encoding='utf-8') as f: #added " encoding='utf-8' "
         for page in range(1, 1000):
             response = requests.get(url=url, headers=headers).json()
             contacts = response["data"]
             for contact in contacts:
                 target = "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (contact["company_name"], contact["location"], contact["summary"], contact["job_title"], contact["name"], contact["job_industry"], contact["email"], contact["first_name"], contact["last_name"], contact["notes"])
                 f.write(target + "\n")
                 print(target)

I keep receiving key error 'Notes'. Notes is in a nested response. How do I solve this? I have included a sample of the json.
Traceback added by request.

    Traceback (most recent call last):
  File "/Users/xxxxxx/Documents/code/trade show w notes", line 16, in <module>
    target = "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (contact["company_name"], contact["location"], contact["summary"], contact["job_title"], contact["name"], contact["job_industry"], contact["email"], contact["first_name"], contact["last_name"], contact["notes"])
KeyError: 'notes'

\

"data": [
    {
        "team_id": 53806,
        "name": "Nicholas Bancroft Cooke",
        "first_name": "Nicholas",

        ],
        "email": null,
        "metadata": null,
        "qualification": [
            {
                "qualification_id": 17573056,
                "qualification": "connected",

                "notes": null,

\\

     page = 1
     url = "https://teams-api.grip.events/1/team/53806/event/123721/member/236388/contact/inbound_lead/reviewed?page=1"
     headers = {
         'authorization': 'Bearer eaf3bd4b-6861-4ca2-a86e-3a96c73deac0',
}
data = ["company_name", "job_title", "name",  "job_industry", "summary", "notes", "location", "first_name", "last_name", "email"]
with open("list31.txt", "w", encoding='utf-8') as f: #added " encoding='utf-8' "
         for page in range(1, 1000):
             response = requests.get(url=url, headers=headers).json()
             contacts = response["data"]
             for contact in contacts:
                 target = "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (contact["company_name"], contact["location"], contact["summary"], contact["job_title"], contact["name"], contact["job_industry"], contact["email"], contact["first_name"], contact["last_name"], contact["notes"])
                 f.write(target + "\n")
                 print(target)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ偶尔ゞ 2025-02-16 18:35:24

如果您指的是Notes键 priblefification 。

然后应该是:联系[“资格”] [0] [“ notes”]

此处联系[conficification> contact [“ priquification”]是列表。如果有机会联系[“资格”是一个空列表,它将提出indexError。您需要使用try-catch进行处理。

If you are referring to the notes key which is under qualification.

Then it should be: contact["qualification"][0]["notes"]

Here contact["qualification"] is a list. if by any chance contact["qualification"] is an empty list it will raise an IndexError. you need to handle it with a try-catch or if-clause

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文