繁体   English   中英

jQuery-在脚本标签中解析JSON

[英]jQuery - Parsing JSON inside script tag

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

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错误的SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data得到了SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data 我检查了http://api.jquery.com/jquery.parsejson/ ,并将$(dom).html()的结果列为$(dom).html()的JSON字符串结果之一。 如何使用jQuery解析JSON?

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

将您的HTML更改为:

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

{'1':'2'}是JavaScript对象,不是正确的JSON。

JSON格式的Object是使用双引号引起来的String,例如: '{"1": "2"}'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM