我尝试解析脚本标签内的 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/