简体   繁体   中英

Adding JSON to properties in Google Apps Script

I am trying to add and read Json in Google Apps Script properties service. When I log the following code:

function addTestCompaniesToProperties() {
  var scriptProperties = PropertiesService.getScriptProperties();
  scriptProperties.deleteProperty("company_list");
  var JsonString = '{ "companies":[{ "company_name":"GE","company_id":"1234"},{"company_name":"Apple","company_id":"5678"}]}'
  
  scriptProperties.setProperty("company_list", JsonString);

  var companyData = scriptProperties.getProperty("company_list");
  var companyJson = JSON.stringify(companyData)
  companyJson = companyJson.replace(/\n/, " ");
  Logger.log(companyJson);

}

I get the following log:

[20-08-17 09:01:15:886 PDT] "{ \"companies\":[{ \"company_name\":\"GE\",\"company_id\":\"1234\"},{\"company_name\":\"Apple\",\"company_id\":\"5678\"}]}"

I can't seem the get rid of the new line characters. What am I doing wrong in retrieving this Json data?

Since you want to escape \ you should try this instead:

companyJson = companyJson.replace(/\\/g, '')

or companyJson = companyJson.replace(/\\/g, ' ') .

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