在JavaScript中将JSON数据转换为对象的最佳方法? [英] Best way to convert JSON data to an object in JavaScript?

查看:92
本文介绍了在JavaScript中将JSON数据转换为对象的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

eval ("myObject="+data);

我被告知 eval 是'邪恶'并且可以解决大的安全问题。

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

我想知道 - 正在使用 eval 来转换JSON数据到对象接受的做法?或者有更好的方法吗?

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

推荐答案

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

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.

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

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

链接文字

这篇关于在JavaScript中将JSON数据转换为对象的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆