如何从嵌套数组响应 body 中的一个 API 获取变量?

How to grab variable from one API that is within a nested Array response body?

如何获取 contentID 和内容标题将其添加到数组中并在 API 的 URL 中使用它 2

如果我将下面的代码用于部分标题和 sectionid,它可以正常工作,因为它不在嵌套数组中,但对于 contentid 和 contenttitle,它不起作用,因为它在嵌套数组中。

在 API 1 个测试选项卡中,我有:

for (i = 0; i < resultCount; i++) {
    var id = jsonData[i].contents[0].contentId;
    var modelString = jsonData[i].contents[0].contentTitle;
    console.log(id)
    console.log(modelString)

    if (modelString.includes(“APIAUTOMATIONcontent”) || modelString.includes(“Testcontent”) || modelString.includes("{{POST-NewSlide}}") || modelString.includes(“stringcontent”)) {
        hasDelete.push(id);
        // (id) - this creates an integer (int)
        //   "announcementId": id,   (creating object)
        //   "hasDelete": modelString.includes("Delete") || modelString.includes("Test")
        // });
    } else {
        doesntHaveDelete.push(id)
        //   "announcementId": id
        // });
    }
}

// Check that each object in response contained keyword and length matches from test

pm.test(Number of Content that has APIAUTOMATIONcontent or Test ready to be deleted = ${hasDelete.length} out of Total ${resultCount} , function() {
    console.log(hasDelete);
    console.log(doesntHaveDelete);
    pm.expect(hasDelete.length);
});

pm.collectionVariables.set(‘deletesections’, JSON.stringify(hasDelete));

就像您在响应正文中遍历每个 section 一样,您还需要遍历 contents 数组,您可以像下面那样进行:

for (i = 0; i < resultCount; i++) {

    contentCount = jsonData[i].contents.length;
    for (j = 0; j < contentCount; j++) {
        let content = jsonData[i].contents[j];
        console.log(content.contentId);
        console.log(content.sectionId);
        console.log(content.contentTitle);
        console.log(content.pdfLink);
        console.log(content.videoLink);
        console.log(content.sortOrder);
    }
}