[英]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.