• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

Ajax Using JSON without eval()

I have repeatedly used Ajax to request a JSON formatted data set from a PHP page and then converted this to a JavaScript object with the following statement:

   eval('JSON = ' + req.responseText);

But lately I've become aware of the hazards of the eval() function.  How can I convert my JSON-formattted text to a JavaScript object without the use of eval()?

Thnks.
0
ttheimer
Asked:
ttheimer
  • 3
1 Solution
 
leakim971PluritechnicianCommented:
hello ttheimer,

Include json2.js : <script language="javascript" src="json2.js" ></script>
source link is a the end of this page : http://www.json.org/js.html

direct link to json2.js : http://www.json.org/json2.js

And you are able to use JSON.parse
on any browser.

Regards
var myObject = JSON.parse( req.responseText );

Open in new window

0
 
leakim971PluritechnicianCommented:
On recent browsers you don't need json2.js so you don't need to "include" the source code of the parser and the stringifier
0
 
ttheimerAuthor Commented:
Interesting that the JSON.parse method uses the eval() function so it looks like, in the end, there is no way around this.
0
 
leakim971PluritechnicianCommented:
Thanks for the points!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now