How to load data from external json file(.json) which contains a variable defined in it.
var mainObject= {"main":[{key1:value2}, {key2:value2}]}
What is the method to call mainObject
vaiable using jquery or javascript?
How to load data from external json file(.json) which contains a variable defined in it.
var mainObject= {"main":[{key1:value2}, {key2:value2}]}
What is the method to call mainObject
vaiable using jquery or javascript?
var jsonData=JSON.parse(mainObject);
var data=jsonData.main;
console.log(data[0].key1);
console.log(data[1].key2);
I assume you have json data in mainObject so calling in javascript like this
console.log(mainObject[0].key1);
As the jQuery API says: "Load JSON-encoded data from the server using a GET HTTP request."
http://api.jquery.com/jQuery.getJSON/ So you cannot load a local file with that function.
Either $.ajax or $.get to grab a json file. Basically you can do something like:
$.ajax({
url: 'file.json',
method: 'GET'
}).success(function(data){
var property = JSON.parse(data).property;
});
This is assuming that the json file is properly formatted. A valid JSON file with your data would look more like:
{
"main": [
{
"key1": "value1"
},
{
"key2": "value2"
}]
}