简体   繁体   English

如何在不更改 Google Apps 脚本的情况下进行 Json 解析

[英]How to make a Json parse without changes in Google Apps Script

I make a request to Go to Webinar API in Apps Script and the response is something like this:我在 Apps Script 中请求 Go to Webinar API,响应如下:

{"joinUrl":"https://example.com","asset":true,"registrantKey":3669516009270899211,"status":"APPROVED"}

When I make a JSON.parse i get something like this:当我制作 JSON.parse 时,我得到如下信息:

{joinUrl=https://example.com, asset=true, registrantKey=3.6695160092708992E18, status=APPROVED}

The RegistrantKey changes, I dont know why. RegistrantKey 变了,不知道为什么。

Thats my code:那是我的代码:

      try{
    var resp =  UrlFetchApp.fetch(url,options)
    var json=JSON.parse(resp);
    return json
  }catch(err){
    Logger.log(err);
    return err;

} }

How about this answer?这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Issue and workaround:问题和解决方法:

I think that the reason of your issue is that 3669516009270899211 of "registrantKey":3669516009270899211 is used as the number.我认为你的问题的原因是3669516009270899211"registrantKey":3669516009270899211作为数。 In Javascript, the maximum integer value is 9007199254740991 .在 Javascript 中,最大整数值为9007199254740991 Ref So when 3669516009270899211 is converted to the number, it becomes 3.6695160092708992E18 and when this is converted to the string, it becomes 3669516009270899000 . Ref因此,当3669516009270899211转换为数字时,它变为3.6695160092708992E18 ,当将其转换为字符串时,它变为3669516009270899000

So in this case, as one of several workaround, how about enclosing 3669516009270899211 by the double quotes like "registrantKey":"3669516009270899211" ?因此,在这种情况下,作为几种解决方法之一,如何将3669516009270899211用双引号括起来,例如"registrantKey":"3669516009270899211" By this, "3669516009270899211" is used ad the string, and you can retrieve the value of 3669516009270899211 .通过这种方式, "3669516009270899211"用于字符串,您可以检索3669516009270899211的值。

Modified script:修改后的脚本:

When your script is modified, how about the following modification?当你的脚本被修改时,下面的修改如何?

From: 从:
 var resp = UrlFetchApp.fetch(url,options) var json=JSON.parse(resp); return json
To: 到:
 var resp = UrlFetchApp.fetch(url,options); resp = resp.getContentText().replace(/registrantKey":(\\d.+),/, "registrantKey\\":\\"$1\\","); // Added var json=JSON.parse(resp); return json

Note:笔记:

  • As the test script, you can also use the following script.作为测试脚本,您还可以使用以下脚本。

     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)

Reference:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM