首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用google脚本获取json上的所有列表

如何使用google脚本获取json上的所有列表
EN

Stack Overflow用户
提问于 2022-08-29 14:23:18
回答 1查看 113关注 0票数 0

我是GAS的初学者,我正在寻求帮助来完成我的脚本,我的目标是从Public Api获得数据,实际上我能够编写一个代码来检索数据,但是我仍然不能从数组中提取所有的数据,例如:

代码语言:javascript
复制
"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“

我的脚本图片:在这里输入图像描述

代码语言:javascript
复制
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}')
  }

有人能帮我改进剧本吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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" ..
  • 在您的情况下,从脚本和显示数据来看,它假设来自API的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的原因。

在本例中,下面的示例脚本如何?

发自:

代码语言:javascript
复制
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;
}

至:

代码语言:javascript
复制
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'],,,]。如果要将结果检索为一维数组,请按以下方式修改它。
代码语言:javascript
复制
- From

var result = data.map(({isLabeledBy}) => isLabeledBy.map(o => o.isConceptualizedBy?.label x+“));

代码语言:javascript
复制
- To

var result = data.flatMap(({isLabeledBy}) => isLabeledBy.map(o => o.isConceptualizedBy?.label x+“));

  • 或者,如果要将结果值放在一列中,请使用 变量result = data.flatMap(({isLabeledBy}) => isLabeledBy.map(o => o.isConceptualizedBy?.label连体“”);

注意:

  • 这个修改后的脚本假设您可以从API中检索值。请小心这件事。

参考资料:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73530300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档