我是GAS的初学者,我正在寻求帮助来完成我的脚本,我的目标是从Public Api获得数据,实际上我能够编写一个代码来检索数据,但是我仍然不能从数组中提取所有的数据,例如:
"isLabeledBy": [
{
"attributedAt": null,
"endDate": null,
"detail": [],
"isConceptualizedBy": {
"code": "10055",
"pictureUrl": "https://smedia.alkemics.com/api/1/concept/10055/picture/logo/original.png",
"description": "",
"label": "Cosmebio"
},
"value": null,
"startDate": null
},
{
"attributedAt": null,
"endDate": null,
"detail": [],
"isConceptualizedBy": {
"code": "80002",
"pictureUrl": "https://smedia.alkemics.com/api/1/concept/80002/picture/logo/original.png",
"description": "",
"label": "Triman"
},
"value": null,
"startDate": null
},
{
"attributedAt": null,
"endDate": null,
"detail": [],
"isConceptualizedBy": {
"code": "ECOCERT_COSMETIQUE_ECO",
"pictureUrl": "https://smedia.alkemics.com/api/1/concept/100230755/picture/logo/original.png",
"description": "",
"label": "Ecocert - Cosmétique Écologique"
},
"value": null,
"startDate": null
},
{
"attributedAt": null,
"endDate": null,
"detail": [],
"isConceptualizedBy": {
"code": "19095",
"pictureUrl": "https://smedia.alkemics.com/api/1/concept/19095/picture/logo/original.png",
"description": "",
"label": "European Organic Agriculture"
},
"value": null,
"startDate": null
},
{
"attributedAt": null,
"endDate": null,
"detail": [],
"isConceptualizedBy": {
"code": "72117",
"pictureUrl": "https://smedia.alkemics.com/api/1/concept/72117/picture/logo/original.png",
"description": "",
"label": "Concours des vins Elle à Table 2017 - Or"
},
"value": null,
"startDate": null
}
],这里有一个json,我需要与属性标签相对应的数据,但是我的脚本允许我只获取数组中的第一个,而不能获得另外四个数组(我可以通过更改脚本中的索引而不是同时更改第五个索引来获得其他数据)。
第一个:“标签”:"Cosmebio“
其他四种:“标签”:"Concours des vins Elle Table 2017 -或“标签”:“欧洲有机农业”标签“:"Ecocert - Cosmétique”“label”"label":"Triman“
我的脚本图片:在这里输入图像描述
var lesparams =params + '&limit=' + limit + '&next_page=' + next_page + '&ts=' + Math.floor((Math.random() * 10000000) + 1);
var url='https://apis.alkemics.com/public/v1/products?'+lesparams;
var content =UrlFetchApp.fetch(url, options);
if (content.getResponseCode() ==200) {
var retour =JSON.parse(content.getContentText());
next_page=retour.next_page;
var data=retour.data;
for(i=0; i<data.length;i++) {
var produit=data[i]; // Un produit au format JSON
var line=[
produit.isLabeledBy[0] != null? produit.isLabeledBy[0].isConceptualizedBy.label:'',
];
produits.push(ligne);
total=total+1;
}
}
else {
Logger.log('Error in GET /contents/{contentid}')
}有人能帮我改进剧本吗?
谢谢
发布于 2022-08-30 06:31:47
我相信你的目标如下。
All the values corresponding to the attribute Label : "label": "Cosmebio" , "label": "Triman" "label": "Ecocert - Cosmétique Écologique" ..的答复和您的问题中,您希望从显示数据中检索"Cosmebio" , "Triman", "Ecocert - Cosmétique Écologique" ..。content.getContentText()的值可能是{"data": [{"isLabeledBy": [### your showing data ###]},{"isLabeledBy": [### your showing data ###]},,,]}。如果我的猜测是正确的,那么在您当前的脚本中,每个循环都使用produit.isLabeledBy[0]。这样,只检索了"isLabeledBy"的第一个元素。我认为这可能是您发布but my script allows me to get only the first one in the array but not the four others的原因。
在本例中,下面的示例脚本如何?
发自:
var data=retour.data;
for(i=0; i<data.length;i++) {
var produit=data[i]; // Un produit au format JSON
var line=[
produit.isLabeledBy[0] != null? produit.isLabeledBy[0].isConceptualizedBy.label:'',
];
produits.push(ligne);
total=total+1;
}至:
var data = retour.data;
var result = data.map(({isLabeledBy}) => isLabeledBy.map(o => o.isConceptualizedBy?.label || ""));data的每个元素返回。就像[['Cosmebio','Triman','Ecocert - Cosmétique Écologique','European Organic Agriculture','Concours des vins Elle à Table 2017 - Or','Cosmebio'],,,]。如果要将结果检索为一维数组,请按以下方式修改它。- Fromvar result = data.map(({isLabeledBy}) => isLabeledBy.map(o => o.isConceptualizedBy?.label x+“));
- Tovar result = data.flatMap(({isLabeledBy}) => isLabeledBy.map(o => o.isConceptualizedBy?.label x+“));
注意:
参考资料:
https://stackoverflow.com/questions/73530300
复制相似问题