简体   繁体   English

使用特定信息将 JSON 中的数据提取到谷歌表格中

[英]Taking data from JSON into google sheets using specific info

I'm so bad at titles and I don't know the technical terms, sorry about that.我的标题很糟糕,我不知道技术术语,对此感到抱歉。

I've managed to take the data from here , but how do I make it only pull data from "hq":true ?我已经设法从这里获取数据,但是如何让它只从"hq":true中提取数据? This is what I have so far:这是我到目前为止所拥有的:

=ImportJSON("https://universalis.app/api/Ultros/36105","/listings/pricePerUnit","noHeaders")

I assume I can't just /"hq":true because the hq and pricePerUnit is in the same...row?我认为我不能只是 /"hq":true 因为 hq 和 pricePerUnit 在同一行中? If that makes sense.如果这是有道理的。 So do I comma it?那我用逗号吗? /pricePerUnit,"hq":true? /pricePerUnit,"hq":true? I only want it to pull the hq data to my google sheet.我只希望它把总部数据拉到我的谷歌表中。 Is this possible?这可能吗? Thank you.谢谢你。

I believe your goal is as follows.我相信你的目标如下。

  • You want to retrieve the values of the property of listings from the URL of https://universalis.app/api/Ultros/36105 .您想从https://universalis.app/api/Ultros/36105的 URL 中检索listings属性的值。 At that time, you want to retrieve the values with "hq":true .那时,您想使用"hq":true检索值。

In your situation, how about directly preparing a Google Apps Script for retrieving the values?在您的情况下,如何直接准备一个 Google Apps 脚本来检索值? When this is reflected in a script, it becomes as follows.当这反映在脚本中时,它变成如下。

Sample script:示例脚本:

Please copy and paste the following script to the script editor of Google Spreadsheet and save the script.请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中并保存该脚本。 And, when you use this script, please put a custom function of =SAMPLE("https://universalis.app/api/Ultros/36105") to a cell.并且,当您使用此脚本时,请将=SAMPLE("https://universalis.app/api/Ultros/36105")的自定义 function 放入单元格。

function SAMPLE(url) {
  const res = UrlFetchApp.fetch(url);
  const obj = JSON.parse(res.getContentText());
  const v = obj.listings.filter(({hq}) => hq === true);
  const header = Object.keys(v[0]);
  return [header, ...v.map(o => header.map(h => Array.isArray(o[h]) ? o[h].join(",") : o[h]))];
}

Result:结果:

When this script is run, the following result is obtained.运行此脚本时,将获得以下结果。

在此处输入图像描述

Note:笔记:

  • When you want to change the order of columns, please replace Object.keys(v[0]) with the array including the header values you want to use.当您想更改列的顺序时,请将Object.keys(v[0])替换为包含您要使用的 header 值的数组。

  • If you don't want to add the header, please modify return [header, ...v.map(o => header.map(h => Array.isArray(o[h])? o[h].join(","): o[h]))];如果不想添加 header,请修改return [header, ...v.map(o => header.map(h => Array.isArray(o[h])? o[h].join(","): o[h]))]; to return v.map(o => header.map(h => Array.isArray(o[h])? o[h].join(","): o[h])); return v.map(o => header.map(h => Array.isArray(o[h])? o[h].join(","): o[h]));

  • This sample script is for https://universalis.app/api/Ultros/36105 of your question.此示例脚本适用于您的问题的https://universalis.app/api/Ultros/36105 So when you change the URL, this script might not be able to be used.所以当你更改 URL 时,这个脚本可能无法使用。 Please be careful about this.请注意这一点。

  • From /listings/pricePerUnit , when you want to retrieve the value of pricePerUnit when "hq":true , you can also use the following script./listings/pricePerUnit中,当您想在"hq":true时检索pricePerUnit的值时,您还可以使用以下脚本。

     function SAMPLE(url) { const res = UrlFetchApp.fetch(url); const obj = JSON.parse(res.getContentText()); return obj.listings.filter(({hq}) => hq === true).map(({pricePerUnit}) => [pricePerUnit]); }

References:参考:

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

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