【问题标题】:How to parse nested JSON object?如何解析嵌套的 JSON 对象?
【发布时间】:2022-01-01 21:37:08
【问题描述】:

我正在 HubSpot 中开发一个返回嵌套 JSON 的新项目,如下例所示。我正在尝试访问关联的联系人 ID,但正在努力正确引用它(我正在寻找的 id 是下面示例中的值“201”)。我已经把这个脚本放在一起,但是这个脚本只返回 JSON 的整个关联部分,我只想要 id。如何正确引用 id?

这是脚本的输出:

{'contacts': {'paging': None, 'results': [{'id': '201', 'type': 'ticket_to_contact'}]}}

这是我整理的脚本:

import hubspot
from pprint import pprint

client = hubspot.Client.create(api_key="API_KEY")

try:
    api_response = client.crm.tickets.basic_api.get_page(limit=2, associations=["contacts"], archived=False)

    for x in range(2):
        pprint(api_response.results[x].associations)
        
except ApiException as e:
    print("Exception when calling basic_api->get_page: %s\n" % e)

这是完整 JSON 的样子(为了便于阅读,“联系人”属性被缩短):

{
  "results": [
    {
      "id": "34018123",
      "properties": {
        "content": "Hi xxxxx,\r\n\r\nCan you clarify on how the blocking of script happens? Is it because of any CSP (or) the script will decide run time for every URL’s getting triggered from browser?\r\n\r\nRegards,\r\nLogan",
        "createdate": "2019-07-03T04:20:12.366Z",
        "hs_lastmodifieddate": "2020-12-09T01:16:12.974Z",
        "hs_object_id": "34018123",
        "hs_pipeline": "0",
        "hs_pipeline_stage": "4",
        "hs_ticket_category": null,
        "hs_ticket_priority": null,
        "subject": "RE: call followup"
      },
      "createdAt": "2019-07-03T04:20:12.366Z",
      "updatedAt": "2020-12-09T01:16:12.974Z",
      "archived": false
    },
    {
      "id": "34018892",
      "properties": {
        "content": "Hi Guys,\r\n\r\nI see that we were placed back on the staging and then removed again.",
        "createdate": "2019-07-03T07:59:10.606Z",
        "hs_lastmodifieddate": "2021-12-17T09:04:46.316Z",
        "hs_object_id": "34018892",
        "hs_pipeline": "0",
        "hs_pipeline_stage": "3",
        "hs_ticket_category": null,
        "hs_ticket_priority": null,
        "subject": "Re: Issue due to server"
      },
      "createdAt": "2019-07-03T07:59:10.606Z",
      "updatedAt": "2021-12-17T09:04:46.316Z",
      "archived": false,
      "associations": {
        "contacts": {
          "results": [
            {
              "id": "201",
              "type": "ticket_to_contact"
            }
          ]
        }
      }
    }
  ],
  "paging": {
    "next": {
      "after": "35406270",
      "link": "https://api.hubapi.com/crm/v3/objects/tickets?associations=contacts&archived=false&hs_static_app=developer-docs-ui&limit=2&after=35406270&hs_static_app_version=1.3488"
    }
  }
}

【问题讨论】:

  • 本文档可能有用realpython.com/python-json
  • @martineau,谢谢,我看过其中的一些,我还有一个 100 天的 Python 课程,涵盖了一点,我面临的挑战是如何访问每种对象类型。通过每个错误,我能够想出正确的答案,即: api_response.results[x].associations["contacts"].results[0].id to get the first id
  • 我认为你最后的评论一定是给@msanford,而不是我。

标签: python json api hubspot


【解决方案1】:

你可以api_response.results[x].associations["contacts"]["results"][0]["id"]

【讨论】:

  • 谢谢,我试过了,但我得到了错误:pprint(api_response.results[x].associations["contacts"]["results"][0]["id"]) TypeError:“NoneType”对象不可下标
  • type(api_response.results[x].associations) 的输出是什么?我最初以为它是一本字典,但基于您使用点表示法这一事实,我可能是错的。
  • 输出为: 。在经历了所有类型错误之后,我能够对其进行整理。我是这种嵌套类型的新手,但看起来关键是了解我要引用的类型
【解决方案2】:

整理出来,张贴以防其他人正在为 HubSpot v3 Api 的响应而苦苦挣扎。此调用的响应模式是:


    Response schema type: Object
    String  results[].id
    Object  results[].properties
    String  results[].createdAt
    String  results[].updatedAt
    Boolean results[].archived
    String  results[].archivedAt
    Object  results[].associations
    Object  paging
    Object  paging.next
    String  paging.next.after
    String  paging.next.linkResponse schema type: Object
    String  results[].id
    Object  results[].properties
    String  results[].createdAt
    String  results[].updatedAt
    Boolean results[].archived
    String  results[].archivedAt
    Object  results[].associations
    Object  paging
    Object  paging.next
    String  paging.next.after
    String  paging.next.link

因此,要访问与工单关联的联系人的 ID,您需要使用以下表示法来引用它:

api_response.results[1].associations["contacts"].results[0].id

注释:

  • results[x] - 在索引中引用结果
  • 关联[“联系人”] - 关联是一个字典对象,你可以访问联系人项 以它的名字命名
  • associations["contacts"].results 是一个列表 - 参考 通过索引 []
  • id - 是一个字符串

【讨论】:

    【解决方案3】:

    在我的情况下,typeModelPropertyCollectionResponseProperty 无论如何都无法到达 dict

    为了记录,这让我查看了结果。

    for result in list(api_response.results):
       ID = result.id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 2020-05-18
      • 2019-12-18
      • 2019-08-28
      相关资源
      最近更新 更多