views:

192

answers:

3

I'm developing a simple JavaScript (jQuery) slide show. I wanted to use JSON to store some some static parameters about the content of each slide.

Since I like to keep my data separate from my code, is there anyway to have JavaScript evaluate a .json file?

  • AJAX seems a bit overkill - it just needs to evaluate it at run-time, nothing is dynamic.
  • I could have a separate .js file that holds my JSON data as an object - but this seems messy.

So before I use one of the above solutions, I just wanted to make sure there wasn't a cleaner way to have JavaScript evaluate a text file.

+2  A: 

What's wrong with having a separate js file for your object? It has to live somewhere. I agree ajax is overkill, but I'm at a loss for why you think a JavaScript object in a .js file is "messy."

Nosredna
Agreed. Having it in a JS file is similar to having it stored anywhere else - think of config files or XML, etc. - Nothing wrong with it.
Jani Hartikainen
In fact it makes your life less messy!
Shankar Ramachandran
So you don't want it in your code, retrieved by code, or linked? That pretty well leaves it in a hidden input, which is even tackier. I'm with these guys: an external .js is the cleanest option.
Brother Erryn
Ah ok , it's not extremely messy and that's what I was leaning towards - I just wasn't sure if there was a cleaner way to evaluate a file, this way your data is truly kept separate from your code. I mean having a .js file with var JSON = "..."; isn't that big of a deal, but it kind of goes against the whole semantic web ideology, regardless of the fact that no one else will be reusing this data.
KeyboardInterrupt
How is the data for the slides stored?
Nosredna
A: 

Evaluating JSON is very easy:

var jsonString = "{'name': 'Joe', 'age': 36}";

var data = eval('(' + jsonString + ')');
data.name // 'Joe'
data.age  // 36

The easiest way to store this data is to put it in an <input type="hidden"> and then read it using document.getElementById('hiddenElementId').value.


The complete picture:

HTML

<input type="hidden" id="hiddenElementId" value="{'name': 'Joe', 'age': 36}" />

JS

function getData() {
     var jsonString = document.getElementById('hiddenElementId').value;
     var data = var data = eval('(' + jsonString + ')');
     return data;
}

It's not actually necessary to be a hidden element - you can put it in an attribute in one of the images.


Another option is to stick it into a <script> tag under some name.

<script type="text/javascript">
    var data = {'name': 'Joe', 'age': 36};
</script>

This way data becomes a global variable (something I don't like so much) and can be used from everywhere. It's a simple solution, but a bit messy - I would go with the first one.

Emil Ivanov
To evaluate a JSON string you can use `JSON.parse(string)` although I don't know about IE.
Philipe Fatio
Fr all browsers you can use JSON.parse if you use the JSON library. Most people frown upon the eval. Newer browsers have a "safe" parse.
Nosredna
A: 

J-Query has a parse JSON function that even works in IE..

$.parseJSON("{'json':'rules for data'}");

Enjoy..

but just a question how to make it tidy like:

FUNCTION_HOWTO({json:'test',p2:'part2'}) = {
    json:'test',
    p2:'part two'
}
JamesM