2

I am trying to load a Local JSON file by jquery. the code works fine but the data are not available in array.

$.getJSON("/ajax/data/myjasonfile.json", function(json) {
        console.log(json);
    });

my console shows only

Object {data: Array[1]}

my sample json

{
    "data": [
        {
            "number": "1234",
            "nameOne": "Laten Thamn",
            "type": "Fishing Vessels",
            "flagName": "5467",
            "buildDate":"12/08/2016"
        }
    ]
}
Liam
  • 27,717
  • 28
  • 128
  • 190
shaheer
  • 50
  • 1
  • 1
  • 9

4 Answers4

3

Try this to understand your response structure.

 $.getJSON("myjasonfile.json", function(json) {
        console.log(json); // access the response object
        console.log(json.data); // access the array
        console.log(json.data[0]); // access the first object of the array
        console.log(json.data[0].number); // access the first object proprty of the array
    });
Deep
  • 9,594
  • 2
  • 21
  • 33
0

jQuery.getJSON() load JSON-encoded data from the server using a GET HTTP request.

As you post in your JSON structure you can access to the array by calling json.data:

$.getJSON("/ajax/data/myjasonfile.json", function(json) {
     console.log(json.data); // Logs your array
});
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
0

Store The json Data in Array:

 $.getJSON("/ajax/data/myjasonfile.json", function(Data) {
        var array=[];
    for(var i=1;i<Data.length;i++)
    {
        var b=Data[i];
        var c=[];
        c.push(b.number);
        c.push(b.nameOne);
        c.push(b.type);
        c.push(b.flagName);
        c.push(b.buildDate);
        array.push(c);
    }
        console.log(array);
    });
Santhosh
  • 140
  • 2
  • 13
  • 1
    May I suggest you add some context explaining your answer? – Neo Aug 07 '17 at 12:38
  • 1
    Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its educational value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Aug 07 '17 at 14:11
-1

you can try this:

$.getJSON("test.json", function(json) {
    console.log(json); // this will show the info it in firebug console
});

I think, Some issue is in your path to call json file. you need to check again of path.

You can check this link: Loading local JSON file

Community
  • 1
  • 1
kishan Radadiya
  • 788
  • 6
  • 14