gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:30:51 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com