Jul*_*lio 1 json request google-apps-script jsonparser
我在 Apps Script 中请求 Go to Webinar API,响应如下:
{"joinUrl":"https://example.com","asset":true,"registrantKey":3669516009270899211,"status":"APPROVED"}
Run Code Online (Sandbox Code Playgroud)
当我制作 JSON.parse 时,我得到如下信息:
{joinUrl=https://example.com, asset=true, registrantKey=3.6695160092708992E18, status=APPROVED}
Run Code Online (Sandbox Code Playgroud)
RegistrantKey 变了,不知道为什么。
那是我的代码:
try{
var resp = UrlFetchApp.fetch(url,options)
var json=JSON.parse(resp);
return json
}catch(err){
Logger.log(err);
return err;
Run Code Online (Sandbox Code Playgroud)
}
这个答案怎么样?请将此视为几种可能的答案之一。
我认为你的问题的原因是,3669516009270899211中"registrantKey":3669516009270899211用作数字。在 Javascript 中,最大整数值为9007199254740991。Ref所以当3669516009270899211被转换为数字时,它变成了3.6695160092708992E18,当它被转换为字符串时,它变成了3669516009270899000.
所以在这种情况下,作为几种解决方法之一,3669516009270899211用双引号括起来"registrantKey":"3669516009270899211"怎么样?通过这种方式,"3669516009270899211"使用 ad 字符串,您可以检索 的值3669516009270899211。
当你的脚本被修改时,下面的修改如何?
从:var resp = UrlFetchApp.fetch(url,options)
var json=JSON.parse(resp);
return json
Run Code Online (Sandbox Code Playgroud)
到:
var resp = UrlFetchApp.fetch(url,options);
resp = resp.getContentText().replace(/registrantKey":(\d.+),/, "registrantKey\":\"$1\","); // Added
var json=JSON.parse(resp);
return json
Run Code Online (Sandbox Code Playgroud)
作为测试脚本,您还可以使用以下脚本。
var str = '{"joinUrl":"https://example.com","asset":true,"registrantKey":3669516009270899211,"status":"APPROVED"}';
str = str.replace(/registrantKey":(\d.+),/, "registrantKey\":\"$1\",");
var obj = JSON.parse(str);
Logger.log(obj)
Run Code Online (Sandbox Code Playgroud)如果我误解了您的问题并且这不是您想要的方向,我深表歉意。