1

i need help with json file and other array. I wish to push objects in my array, and then alert value of some object...

var jsonOriginal = new Array('http://192.168.1.101:8080/mbx/labResults.json');
var jsonDisplay = new Array();

function grid ()  {
    $.getJSON(jsonOriginal, function(data) {
         jsonLength = data.length;
         jsonDisplay.push(data);
    });
alert(jsonDisplay[0].someone.name);

example of json:

[{"someone":{"name":"Ljubica Lulik","number":"523011571"}, {"someone":{"name":"Lubi Lulik","number":"523012341"}]
1
  • 1
    What is your problem? You cannot load json data? Commented Mar 30, 2012 at 12:36

2 Answers 2

3

push pushes a single value into the array. If you want to have the elements of your first array + the elements of a second array you have to use concat:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/concat

0
1

You are providing an array not a string as URL for $.getJSON. URL needs to be a string. It is not clear at all why you have the URL in an array

AJAX is asynchronous so when you try to alert the data the ajax hasn't completed yet. To access the data you need to do it in the success callback of the ajax

Then your alert of the data needs to index someone

function grid ()  {
    /* get url from array*/
    var url=jsonOriginal[0];

    $.getJSON(url, function(data) {
         jsonLength = data.length;
         jsonDisplay.push(data);
         /* need to fix your json to be valid then can access jsonDisplay[0] here*/

    }); 

         /* can't access new data here...ajax has not completed yet*/

}

EDIT
looking closer at JSON is does not validate in jsonlint.com. Structure of it isn't very friendly either

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.