Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 从外部API获取特定值-谷歌表单_Regex_Api_Google Sheets - Fatal编程技术网

Regex 从外部API获取特定值-谷歌表单

Regex 从外部API获取特定值-谷歌表单,regex,api,google-sheets,Regex,Api,Google Sheets,我使用GoogleSheet,我想从外部api获取特定值。我为每个特定对象(股票)调用API。我想要这个json的第一个值(10.2222)和第四个值(18.08795)。我不能使用th ID的编号,因为每个对象的编号都会改变。我可以使用位置(1°和4°) 谷歌脚本中的正则表达式函数 function importRegex(url, regex_string) { var html, content = ''; var response = UrlFetchApp.fetch(

我使用GoogleSheet,我想从外部api获取特定值。我为每个特定对象(股票)调用API。我想要这个json的第一个值(10.2222)和第四个值(18.08795)。我不能使用th ID的编号,因为每个对象的编号都会改变。我可以使用位置(1°和4°)

谷歌脚本中的正则表达式函数

    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)
返回j_obj[“数据”][n][“属性”][“值”]
其中
n
是需要检索的值的id

完整方法:

函数rss(url,n){
var params={
标题:{
“内容类型”:“应用程序/json”;“接受”:“应用程序/json”,
“用户代理”:“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/71.0.3578.98 Safari/537.36”
},
muteHttpExceptions:true,
方法:“获取”,
contentType:“应用程序/json”,
validateHttpsCertificates:false,
};
var response=UrlFetchApp.fetch(url,参数);
html=response.getContentText();
const j_obj=JSON.parse(html);
返回j_obj[“数据”][n][“属性”][“值”];
}
    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;  
}