168
rated 0 times
[
171]
[
3]
/ answers: 1 / hits: 21247
/ 11 Years ago, fri, september 20, 2013, 12:00:00
I have defined a json array in a file as
{
mainServiceBar= [
{ mainServiceName:ECONOMIC SERVICES , mainServiceValue:23.2 },
{ mainServiceName:SOCIAL SERVICES , mainServiceValue:34.5 },
{ mainServiceName:DEFENSE , mainServiceValue:4.5 },
{ mainServiceName:GENERAL PUBLIC SERVICES , mainServiceValue:19 },
{ mainServiceName:DEBT BURDEN , mainServiceValue:18.8 },
];
subServiceBar1: [
{ subServiceName:agriculture , subServiceValue:12.5 },
{ subServiceName:trade , subServiceValue:12.5 },
{ subServiceName:tourism , subServiceValue:12.5 },
{ subServiceName:power and energy , subServiceValue:12.5 },
{ subServiceName:water resource , subServiceValue:12.5 }
{ subServiceName:communication roads , subServiceValue:12.5 }
{ subServiceName:other economic services , subServiceValue:12.5 },
{ subServiceName:subsidy , subServiceValue:12.5 },
];
}
now i want these json arrays values in a javascript function , I have done few things but it I didn't get the values , my code is as follows :
function getJsonArray() {
return $.getJSON(myjson.js);
}
function socialservice(thisv) {
json = getJsonArray();
for (var key in json) {
if (json.hasOwnProperty(key)) {
var item = json[key];
alert(item.mainServiceName);
}
}
return false;
}
**EDITED
I have also tried this :
$.getJSON(myjson.json, function(data) {
alert(val);
var items = [];
$.each(data, function(key, val) {
alert(val);
});
});
but it doesn't alert anything means it do not get json data
It alerts undefined , Please suggest me some way to get the values , Thanks
More From » jquery