如何访问通过数组定位的正确 json 值?

How to access the right json value that is located through an array?

我目前正在使用夹具文件来确保更容易调用正确的值。

 cy.fixture('latestLead.json').then(function (lead) {
            this.lead = lead
        })
My son file is the following:

{
  "status": 0,
  "result": {
    "totalSize": 1,
    "done": true,
    "records": [
      {
        "attributes": {
          "type": "Lead",
          "url": "/services/data/v51.0/sobjects/Lead/111111111"
        },
        "Id": "111111111",
        "Name": "Andres Latest Test"
      }
    ]
  }
}

我试图获得正确值的方法如下:

cy.get(".gLFyf").type(this.lead.result.records.Id)

我可以从结果对象获取 totalSize 或 done,但无法获取高于该对象的任何其他值。如何从记录数组中获取 Id 值?

试试这个 cy.get(".gLFyf").type(this.lead.result.records[0].Id)

您可以使用索引位置(在您的情况下为零)访问数组项(在您的情况下是对象)

cy.get(".gLFyf").type(this.lead.result.records[0].Id)