最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to load a local JSON file? - Stack Overflow

programmeradmin5浏览0评论

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

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"
        }
    ]
}
Share Improve this question edited Dec 22, 2016 at 12:59 Liam 29.8k28 gold badges139 silver badges203 bronze badges asked Dec 22, 2016 at 11:52 shaheershaheer 501 gold badge1 silver badge9 bronze badges 7
  • 1 "the data are not available in array" what do you mean ? – Denys Séguret Commented Dec 22, 2016 at 11:52
  • Do you mean local to the client machine? If so, this is not possible. If you mean local to the server, then what you have should work fine, assuming your path is valid. – Rory McCrossan Commented Dec 22, 2016 at 11:54
  • The data should be available as an object. What does your console say – ffflabs Commented Dec 22, 2016 at 11:54
  • but i have above 100 records in the json – shaheer Commented Dec 22, 2016 at 11:56
  • you should to check database query. – kishan Radadiya Commented Dec 22, 2016 at 12:08
 |  Show 2 more ments

4 Answers 4

Reset to default 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
    });

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
});

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);
    });

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

发布评论

评论列表(0)

  1. 暂无评论