简体   繁体   中英

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:

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

When I make a JSON.parse i get something like this:

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

The RegistrantKey changes, I dont know why.

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. In Javascript, the maximum integer value is 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 .

So in this case, as one of several workaround, how about enclosing 3669516009270899211 by the double quotes like "registrantKey":"3669516009270899211" ? By this, "3669516009270899211" is used ad the string, and you can retrieve the value of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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