This may be a silly question but I invested more than half an hour to understand why it is not working. I have a 2D javascript array. Some of the elements in array are HTMl having anchor tag with href attributes.
I am trying to used JSON.parse("stringified2D array here") but it give me error as shown in this screenshot.
var cd = JSON.parse('[["header","This is some header"],["footer","<p>This addon is brough to you by <a href=\"https://www.accemy.com\">Accemy</a> and <a href=\"swgapps.com\">SW gApps</a></p>This is universal and appended to all add-on content"],["nslookup","NS Lookup allows to fetch DNS records from public DNS servers"]]');
It gives me error
Uncaught SyntaxError: Unexpected token h in JSON at position 88
at JSON.parse (<anonymous>)
at <anonymous>:1:15

You should escape the quotes twice \\"
var s = '[' +
'["header","This is some header"],' +
'["footer","<p>This addon is brough to you by ' +
'<a href=\\"https://www.accemy.com\\">Accemy</a> and ' + //here
'<a href=\\"swgapps.com\\">SW gApps</a></p>' + //and here
'This is universal and appended to all add-on content"],' +
'["nslookup","NS Lookup allows to fetch DNS records from public DNS servers"]]';
var cd = JSON.parse(s);
console.log(cd.length); //3
You need another \ to escape double quotes in string:
var cd = JSON.parse('[["header","This is some header"],["footer","<p>This addon is brough to you by <a href=\\"https://www.accemy.com\\">Accemy</a> and <a href=\\"swgapps.com\\">SW gApps</a></p>This is universal and appended to all add-on content"],["nslookup","NS Lookup allows to fetch DNS records from public DNS servers"]]');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With