简体   繁体   English

如何在 Google Apps 脚本中访问 JSON 中的特定元素?

[英]How to access a specific element in JSON in Google Apps Script?

I need to access the nav for a specific date from the below JSON. eg: data[date="20-04-2022"].nav How do I do it in Google Apps Script?我需要从以下 JSON 访问特定日期的导航。例如:data[date="20-04-2022"].nav 我如何在 Google Apps 脚本中执行此操作? The standard JSON notation is not working.标准 JSON 符号不起作用。

{
   "meta":{
      "fund_house":"Mutual Fund",
      "scheme_type":"Open Ended Schemes",
   },
   "data":[
      {
         "date":"22-04-2022",
         "nav":"21.64000"
      },
      {
         "date":"21-04-2022",
         "nav":"21.69000"
      },
      {
         "date":"20-04-2022",
         "nav":"21.53000"
      }
   ],
   "status":"SUCCESS"
}

In your situation, I thought that it is required to retrieve the element including "date": "20-04-2022" from the array of data .在您的情况下,我认为需要从data数组中检索包含"date": "20-04-2022"的元素。 So, how about the following sample script?那么,下面的示例脚本怎么样?

Sample script:示例脚本:

 const obj = { "meta": { "fund_house": "Mutual Fund", "scheme_type": "Open Ended Schemes", }, "data": [ { "date": "22-04-2022", "nav": "21.64000" }, { "date": "21-04-2022", "nav": "21.69000" }, { "date": "20-04-2022", "nav": "21.53000" } ], "status": "SUCCESS" }; const search = "20-04-2022"; const res = obj.data.find(({ date }) => date == search); const value = res && res.nav; console.log(value) // 21.53000

  • For example, if the search value is always found, you can use the following script.例如,如果始终找到搜索值,则可以使用以下脚本。

     const res2 = obj.data.find(({ date }) => date == search).nav;

Reference:参考:

Added 1:添加 1:

From your following reply,从您的以下回复中,

This looks like standard java script.这看起来像标准的 java 脚本。 Does not work in google apps script(script.google.com/home).不适用于谷歌应用程序脚本 (script.google.com/home)。 Getting syntax error for this line: const res = obj.data.find(({ date }) => date == search);此行出现语法错误:const res = obj.data.find(({ date }) => date == search);

I'm worried that you are not enabling V8 runtime.我担心您没有启用 V8 运行时。 Ref If you cannot use V8 runtime, how about the following sample script? Ref如果你不能使用 V8 运行时,下面的示例脚本怎么样?

Sample script:示例脚本:

 var obj = { "meta": { "fund_house": "Mutual Fund", "scheme_type": "Open Ended Schemes", }, "data": [ { "date": "22-04-2022", "nav": "21.64000" }, { "date": "21-04-2022", "nav": "21.69000" }, { "date": "20-04-2022", "nav": "21.53000" } ], "status": "SUCCESS" }; var search = "20-04-2022"; var res = obj.data.filter(function (e) { return e.date == search })[0]; var value = res && res.nav; console.log(value) // 21.53000

Added 2:添加2:

From your following reply,从您的以下回复中,

This looks like standard java script.这看起来像标准的 java 脚本。 Does not work in google apps script(script.google.com/home).不适用于谷歌应用程序脚本 (script.google.com/home)。 Getting syntax error for this line: const res = obj.data.find(({ date }) => date == search);此行出现语法错误:const res = obj.data.find(({ date }) => date == search);

I am trying to write a google apps script to fetch data from a url. But google seems to have its own way of handling the JSON data which I am unable to figure out.我正在尝试编写一个谷歌应用程序脚本来从 url 中获取数据。但是谷歌似乎有自己的方式来处理我无法弄清楚的 JSON 数据。 developers.google.com/apps-script/guides/services/… developers.google.com/apps-script/guides/services/...

I understood that your actual goal was to retrieve the value using Web Apps.我了解到您的实际目标是使用 Web Apps 检索值。 If my understanding of your actual goal, how about the following sample script?如果我理解你的实际目标,下面的示例脚本怎么样?

1. Sample script: 1. 示例脚本:

Please copy and paste the following script to the script editor and save the script.请将以下脚本复制并粘贴到脚本编辑器中并保存脚本。

function doGet(e) {
  var search = e.parameter.search;
  var obj = {
    "meta": {
      "fund_house": "Mutual Fund",
      "scheme_type": "Open Ended Schemes",
    },
    "data": [
      {
        "date": "22-04-2022",
        "nav": "21.64000"
      },
      {
        "date": "21-04-2022",
        "nav": "21.69000"
      },
      {
        "date": "20-04-2022",
        "nav": "21.53000"
      }
    ],
    "status": "SUCCESS"
  };
  var res = obj.data.filter(function (e) { return e.date == search })[0];
  var value = res && res.nav;
  return ContentService.createTextOutput(value);
}
  • I think that this sample script can be used with and without V8 runtime.我认为这个示例脚本可以在有和没有 V8 运行时的情况下使用。

2. Deploy Web Apps. 2. 部署 Web 应用程序。

In this case, please check the official document .这种情况请查看官方文档 Please set it as follows.请按如下方式设置。

  • Execute as: Me
  • Anyone with Google account: Anyone

In this case, it supposes that you are using new IDE. Please be careful this.在这种情况下,它假设您使用的是新的 IDE。请注意这一点。

3. Testing. 3. 测试。

Please access to the URL like https://script.google.com/macros/s/{deploymentId}/exec?search=20-04-2022 using your browser.请使用浏览器访问 URL,例如https://script.google.com/macros/s/{deploymentId}/exec?search=20-04-2022 By this, the result value is returned.由此,返回结果值。 ` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM