简体   繁体   中英

Save javascript object into file with break words

I have a big array of objects that contains a string property. When I save my object called data to a file in json, the string property came with breaks words like "getElementsByTagName(\\"track\\")" . I need to get like that: "getElementsByTagName("track")" without \\ .

my code:

       var jsonobj = JSON.stringify(data,null,'\t');
       fs.write('final8.json', jsonobj, 'w');

You can avoid escaping by using single quotes. For example you can replace " with ' using custom serializer.

var data = {test: 'getElementByTagName("track")'}

JSON.stringify(data, function(key,value) {
  if(typeof value === 'string') {
    return value.replace(/"/g, '\'')
  }
  return value
}, '\t');

"{
    "test": "getElementByTagName('track')"
}"

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