0

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!

Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
Jayaram
  • 839
  • 1
  • 14
  • 24

2 Answers2

3

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.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

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

patorjk
  • 2,164
  • 1
  • 20
  • 30
  • Actually I'm not getting the file from the server, its stored locally. Do you know how I can access files stored locally ? – Jayaram Dec 16 '11 at 19:35
  • Diodeus response to this issue above is correct. If you're aiming for HTML5 enabled browsers, do a Google search and you'll find the API. Additionally, the answers here -> http://stackoverflow.com/questions/371875/local-file-access-with-javascript - might also interest you. – patorjk Dec 16 '11 at 19:39