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