0

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 do\esn't alert anything means it do not get json data

It alerts undefined , Please suggest me some way to get the values , Thanks

2
  • 3
    incorrect json provided Commented Sep 20, 2013 at 7:29
  • It is true that the json is incorrect but the function to read the json is also incorrect, see my answer. Commented Sep 20, 2013 at 7:49

3 Answers 3

1

please change your json file to below (it was wrongly formatted)

{
    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" }
    ]
}

also change the socialservice function code to below it was wrongly retrieving the values :

function socialservice(thisv) {
    $.getJSON("myjson.js", function(json) {
        for (key in json) {
          $.each(json[key], function(k, arrayItem) {
            alert(arrayItem.mainServiceName);
          });
        }
    });
    return false;
}

call the function with socialservice(); anywhere in your code.

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

5 Comments

Still function didn't alert anything (didn't go inside this $.getJSON( "myjson.js", function( json ) { })
this might sound silly but did you call the function ? try the new code.
Yes , I have . Infact alert outside this $.getJSON() is working but not inside
the only reason I can think of is that the json (js) file is not in the right place.
may be , but its in the same directory , let me define full path to file
1

Accordin with getJson doc you should write a callback in order to manipulate the data.

$.getJSON( "myjson.js", function( json ) {
    for (var key in json) {
          // code
    }
});

NB: Your JSON code posted is invalid. There are a lot of errors. Try to replace with this:

{
    "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" }
    ]
}

1 Comment

I have tried that to but , it didn't go inside this , I have edited my question please check
0

Change it to this:

function getJsonArray(){
    $.ajax({
        url : 'myjson.js',
        type: 'GET',
        success : socialservice
    })
}


function socialservice(data){

json= data;
for (var key in json) {
    if (json.hasOwnProperty(key)) {
    var item = json[key];
    alert(item.mainServiceName); }}
return false;
  }

Comments

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.