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

问题描述

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

 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)