javascript - 无法解析包含 HTML 的字符串化数组
问题描述
这可能是一个愚蠢的问题,但我花了半个多小时来了解它为什么不起作用。我有一个 2D javascript 数组。数组中的一些元素是 HTMl,具有带 href 属性的锚标记。我正在尝试使用JSON.parse("stringified2D array here")
,但它给了我错误,如此屏幕截图所示。
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"]]');
它给了我错误
Uncaught SyntaxError: Unexpected token h in JSON at position 88
at JSON.parse (<anonymous>)
at <anonymous>:1:15
解决方案
你应该两次转义引号\\"
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
推荐阅读
- julia - 具有邻接矩阵的 Dijkstra 算法
- php - 我想复制 $friendname=$_POST['friendname']; 当 if 语句有效并在此处回显时,值到下一页
- xamarin - 在 Xamarin Forms 中使用邮件服务时,我们如何获取 SmtpServer NetworkCredentials?
- ruby - 无法安装 vagrant-hostsupdater
- reactjs - 在 cPanel 上部署 React 应用程序后,Web 目录可见
- python - .replace() 不替换元素
- javascript - WebRTC onicecandidate 永远不会在 chrome 中触发
- ios - 单击另一个按钮 swift 2.2 更改声音按钮图像
- python - 从图像 OpenCv 中删除合并的行
- python - 如何在卷积层之后添加四个 CNN 模型?那么我怎样才能在全连接层中连接呢?