[英]How to make a Json parse without changes in Google Apps Script
我在 Apps Script 中请求 Go to Webinar API,响应如下:
{"joinUrl":"https://example.com","asset":true,"registrantKey":3669516009270899211,"status":"APPROVED"}
当我制作 JSON.parse 时,我得到如下信息:
{joinUrl=https://example.com, asset=true, registrantKey=3.6695160092708992E18, status=APPROVED}
RegistrantKey 变了,不知道为什么。
那是我的代码:
try{
var resp = UrlFetchApp.fetch(url,options)
var json=JSON.parse(resp);
return json
}catch(err){
Logger.log(err);
return err;
}
这个答案怎么样? 请将此视为几种可能的答案之一。
我认为你的问题的原因是3669516009270899211
的"registrantKey":3669516009270899211
作为数。 在 Javascript 中,最大整数值为9007199254740991
。 Ref因此,当3669516009270899211
转换为数字时,它变为3.6695160092708992E18
,当将其转换为字符串时,它变为3669516009270899000
。
因此,在这种情况下,作为几种解决方法之一,如何将3669516009270899211
用双引号括起来,例如"registrantKey":"3669516009270899211"
? 通过这种方式, "3669516009270899211"
用于字符串,您可以检索3669516009270899211
的值。
当你的脚本被修改时,下面的修改如何?
从: var resp = UrlFetchApp.fetch(url,options) var json=JSON.parse(resp); return json
到:
var resp = UrlFetchApp.fetch(url,options); resp = resp.getContentText().replace(/registrantKey":(\\d.+),/, "registrantKey\\":\\"$1\\","); // Added var json=JSON.parse(resp); return json
作为测试脚本,您还可以使用以下脚本。
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)
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.