سؤال

I am working on a project and I was not able to figure out how to do this. I have a json file and need to assign it to a js variable to use the protovis visualization tools.

I tried searching on google but could not find any. Please let me know if someone knew how to do this. Thanks!

هل كانت مفيدة؟

المحلول

Use jQuery's parseJSON().

Description: Takes a well-formed JSON string and returns the resulting JavaScript object. version added: 1.4.1jQuery.parseJSON( json ) jsonThe JSON string to parse. Passing in a malformed JSON string may result in an exception being thrown.

نصائح أخرى

If you're getting the file from your server via the XMLHttpRequest object, you can parse the json using the JSON.parse function:

var myJson = JSON.parse(response);

For browsers that don't support the JSON object you can get a library for it here: http://www.json.org/js.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top