gpt4 book ai didi

regex - 从外部 API 获取特定值 - Google 表格

转载 作者:行者123 更新时间:2023-12-04 07:38:30 25 4
gpt4 key购买 nike

我使用谷歌表,我想从外部 api 获取特定值。我为每个特定对象(股票)调用 API。我想要这个 json 的第一个 (10.2222) 和第四个值 (18.08795)。我不能使用 th ID 的数量,因为它会因每个对象而变化。我可以只使用位置(1° 和 4°)。

{"data":[{"id":"158379","type":"metric","attributes":{"value":10.2222,"meaningful":true,"grade":3},"relationships":{"metric_type":{"data":{"id":"110","type":"metric_type"}}}},{"id":"224845","type":"metric","attributes":{"value":7.663466,"meaningful":true,"grade":6},"relationships":{"metric_type":{"data":{"id":"95","type":"metric_type"}}}},{"id":"54342","type":"metric","attributes":{"value":3.6891754,"meaningful":true,"grade":0},"relationships":{"metric_type":{"data":{"id":"96","type":"metric_type"}}}},{"id":"224846","type":"metric","attributes":{"value":19.084248,"meaningful":true,"grade":0},"relationships":{"metric_type":{"data":{"id":"99","type":"metric_type"}}}},{"id":"162575","type":"metric","attributes":{"value":18.08795,"meaningful":true,"grade":0},"relationships":{"metric_type":{"data":{"id":"100","type":"metric_type"}}}},{"id":"170262","type":"metric","attributes":{"value":21.21323,"meaningful":true,"grade":0},"relationships":{"metric_type":{"data":{"id":"27","type":"metric_type"}}}},{"id":"38556","type":"metric","attributes":{"value":15.68155,"meaningful":true,"grade":0},"relationships":{"metric_type":{"data":{"id":"29","type":"metric_type"}}}},{"id":"68101","type":"metric","attributes":{"value":27.522194,"meaningful":true,"grade":0},"relationships":{"metric_type":{"data":{"id":"31","type":"metric_type"}}}},{"id":"68100","type":"metric","attributes":{"value":20.490818,"meaningful":true,"grade":0},"relationships":{"metric_type":{"data":{"id":"39","type":"metric_type"}}}},{"id":"188788","type":"metric","attributes":{"value":24.493673,"meaningful":true,"grade":0},"relationships":{"metric_type":{"data":{"id":"28","type":"metric_type"}}}}],"included":[{"id":"110","type":"metric_type","attributes":{"field":"dps_yoy"}},{"id":"95","type":"metric_type","attributes":{"field":"dividend_per_share_change_dislpay"}},{"id":"96","type":"metric_type","attributes":{"field":"dividend_lt_fwd_growth"}},{"id":"99","type":"metric_type","attributes":{"field":"div_grow_rate3"}},{"id":"100","type":"metric_type","attributes":{"field":"div_grow_rate5"}},{"id":"27","type":"metric_type","attributes":{"field":"revenue_change_display"}},{"id":"29","type":"metric_type","attributes":{"field":"eps_change_display"}},{"id":"31","type":"metric_type","attributes":{"field":"fcf_per_share_change_display"}},{"id":"39","type":"metric_type","attributes":{"field":"ebitda_change_display"}},{"id":"28","type":"metric_type","attributes":{"field":"ebit_change_display"}}]}
谷歌脚本中的正则表达式函数
    function importRegex(url, regex_string) {
var html, content = '';
var response = UrlFetchApp.fetch(url);
if (response) {
html = response.getContentText();
if (html.length && regex_string.length) {
var regex = new RegExp( regex_string, "i" );
content = html.match(regex)[1];
}
}
content = unescapeHTML(content);
Utilities.sleep(1000); // avoid call limit by adding a delay
return content;
}
谢谢

最佳答案

由于您的输入是有效的 JSON,您可以使用

j_obj = JSON.parse(html)
return j_obj["data"][n]["attributes"]["value"]
哪里 n是您需要检索的值的 ID。
完整方法:
function rss(url, n) {
var params = {
headers: {
'Content-Type': "application/json", 'Accept': "application/json",
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
},
muteHttpExceptions: true,
method: "GET",
contentType: "application/json",
validateHttpsCertificates: false,
};

var response = UrlFetchApp.fetch(url, params);
html = response.getContentText();
const j_obj = JSON.parse(html);
return j_obj["data"][n]["attributes"]["value"];
}

关于regex - 从外部 API 获取特定值 - Google 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67618995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com