问题描述
如何获取contentID和内容标题,将其添加到数组中并在API 2的URL中使用
如果我将以下代码用于节标题和节ID,则它之所以有效,是因为它不在嵌套数组中,但对于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);
}
}