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!

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

jQuery is open-source, go rip out the part you need.
Thanks,Can you also help me how to read the file which is stored locally ?
Browsers cannot read local files (HTML5 local storage is a different issue) . If they could web pages would be poking around your hard drive without authorization.
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

2 Comments

Actually I'm not getting the file from the server, its stored locally. Do you know how I can access files stored locally ?
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 -> stackoverflow.com/questions/371875/… - might also interest you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.