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.
ttheimerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.