I am trying to make a simple sample previewer for my client project.
The list of samples are stored in a JSON file. but the trouble is that I am not too familiar with using JSON, or programming itself, to be honest.
I have written a short test code to see if this JSON file works well, but although I can access sampleData variable from firebug console, the document.write(sampleData.length); line seems to be unable to access sampleData for some reason. The error shows:
TypeError: sampleData is undefined
I suspect it has to do with variable scopes, but I am not sure.
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
var sampleData;
$.getJSON('res/Samples.json',function(data){
sampleData=data;
});
document.write(sampleData.length);
</script>
What should I do to make the sampleData variable to work with the rest of the code?
I'm sorry I cannot disclose the contents of the json file. It contains an array of objects containing information of the products. it looks like
[
{
"aaa" : 000000;
},
{
"bbb" : 111111;
},
{
"ccc" : 222222;
}
]
It's a quite generic form of JSON file, as far as I can tell.