[英]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.