在 JavaScript 中将 JSON 数据转换为对象的最佳方法?

发布于 2024-08-22 12:13:16 字数 243 浏览 3 评论 0原文

我目前正在 JavaScript 中使用 eval 将从服务器返回的 JSON 数据转换为对象。

eval ("myObject="+data);

有人告诉我,eval 是“邪恶的”,可能会带来严重的安全问题。

我想知道 - 使用 eval 将 JSON 数据转换为对象是否是可接受的做法?或者有更好的方法吗?

I am currently using eval in JavaScript to convert JSON data returned from the server into an object.

eval ("myObject="+data);

I've been told that eval is 'evil' and can open up big security problems.

I'm wondering - is using eval to convert JSON data to an object the accepted practice? Or is there a better way?

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

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

发布评论

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

评论(1

温柔戏命师 2024-08-29 12:13:16

eval 被认为是不好的做法的原因是用户可以评估从服务器发送的任何内容。这意味着,如果您有评论论坛,并且用户提交了一些用于评论的 JavaScript 代码,并且您在客户端进行评估,那么您的网站很容易被劫持。

我喜欢 JQuery-Json 插件。您可以使用以下链接进行查看:

链接文本

The reason eval is considered a bad practice is that user can evaluate anything that is sent from the server. This means if you have comments forum and the user submits some JavaScript code for the comments and you eval on the client side then your website can easily be hijacked.

I like the JQuery-Json plug-in. You can check it out using the following link:

link text

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