如何使用jQuery将字符串转换为JSON对象

发布于 2025-02-09 05:13:43 字数 317 浏览 1 评论 0原文

str = {'8': {'name': 'breeze bedsheet ', 'price': 1150, 'qty': 1, 'strid': '8', 'image': 'images/download_1.jpg'}, '10': {'name': 'neat and clean bedsheet ', 'price': 1150, 'qty': 2, 'strid': '10', 'image': 'images/images_2.jpg'}}

(str)

我想将此字符串转换为json对象

obj = json.parse

str = {'8': {'name': 'breeze bedsheet ', 'price': 1150, 'qty': 1, 'strid': '8', 'image': 'images/download_1.jpg'}, '10': {'name': 'neat and clean bedsheet ', 'price': 1150, 'qty': 2, 'strid': '10', 'image': 'images/images_2.jpg'}}

obj = JSON.parse(str)

I want to convert this string to JSON object but when i use JSON.parse method it gives me an error like

uncaught syntaxError: Unexpected token ' in JSON at position 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2025-02-16 05:13:43

这是因为您使用'而不是

str = "{'8': {'name': 'breeze bedsheet ', 'price': 1150, 'qty': 1, 'strid': '8', 'image': 'images/download_1.jpg'}, '10': {'name': 'neat and clean bedsheet ', 'price': 1150, 'qty': 2, 'strid': '10', 'image': 'images/images_2.jpg'}}";

obj = JSON.parse(str.replaceAll("'", '"'));

console.log(obj);

It is because you use ' instead of "

str = "{'8': {'name': 'breeze bedsheet ', 'price': 1150, 'qty': 1, 'strid': '8', 'image': 'images/download_1.jpg'}, '10': {'name': 'neat and clean bedsheet ', 'price': 1150, 'qty': 2, 'strid': '10', 'image': 'images/images_2.jpg'}}";

obj = JSON.parse(str.replaceAll("'", '"'));

console.log(obj);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文