简体   繁体   English

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

[英]Best way to convert JSON data to an object in JavaScript?

I am currently using eval in JavaScript to convert JSON data returned from the server into an object. 我目前在JavaScript中使用eval将服务器返回的JSON数据转换为对象。

eval ("myObject="+data);

I've been told that eval is 'evil' and can open up big security problems. 有人告诉我, eval是“邪恶的”事情,可能会带来严重的安全问题。

I'm wondering - is using eval to convert JSON data to an object the accepted practice? 我想知道-是否使用eval将JSON数据转换为对象是公认的做法? Or is there a better way? 或者,还有更好的方法?

The reason eval is considered a bad practice is that user can evaluate anything that is sent from the server. eval被视为不良做法的原因是用户可以评估从服务器发送的任何内容。 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. 这意味着,如果您拥有评论论坛,并且用户为评论提交了一些JavaScript代码,并且在客户端进行评估,那么您的网站就很容易被劫持。

I like the JQuery-Json plug-in. 我喜欢JQuery-Json插件。 You can check it out using the following link: 您可以使用以下链接进行检查:

link text 连结文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM