javascript - jQuery - 解析脚本标签内的 JSON

标签 javascript jquery json

我尝试解析脚本标签内的 JSON 字符串 <script src='//remotehost/test.js'>{'1':'2'}</script>

dom = document.scripts[document.scripts.length - 1]; //it gets the correct tag, checked
d = $.parseJSON($(dom).html());

但是我得到SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data错误。我检查了http://api.jquery.com/jquery.parsejson/$(dom).html() 的结果被列为格式错误的 JSON 字符串结果之一。如何使用 jQuery 解析 JSON?

最佳答案

问题是它不是有效的 JSON。有效的 JSON 使用双引号。

将 HTML 更改为:

<script src='//remotehost/test.js'>{"1":"2"}</script>

关于javascript - jQuery - 解析脚本标签内的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27425211/

相关文章:

php - Laravel 5 和 Angular 从 Controller 获取 JSON

javascript - 为什么 jquery 选择器返回一个数组而不是元素本身?

javascript - 使用 d3.js 制作分组条形图

javascript - 点击并按住激活全日历日点击事件所需的一天

javascript - 有没有办法将 rxjs 与 ng2-charts 一起使用?

javascript - 如何使用 jQuery 替换文本区域内的属性内容?

jQuery - 如何将多个节点 append 到容器

c - gSOAP:如何在 C 中比较两个值结构(带有 JSON 内容)?

javascript - 从 .php 页面获取 JSON 数据并将其存储在全局变量中

javascript - 如何解决 Cordova Android 应用程序上远程视频帧抓取的污点 Canvas ?