-2

I need help loading a "json" file into an array on my html page.

My "Json" file is called "JsonData.json"

[
    {
        "ID": 1,
        "Name": "John Smith",
        "IDNumber": "7606015012088"
    },
    {
        "ID": 2,
        "Name": "Molly Malone",
        "IDNumber": "8606125033087"
    },
    {
        "ID": 3,
        "Name": "Rianna Chetty",
        "IDNumber": "6207145122087"
    }
]

I need help to load the above "JsonData.json" file into an array which will look like this:

 var myList = [
    {
        "ID": 1,
        "Name": "John Smith",
        "IDNumber": "7606015012088"
    },
    {
        "ID": 2,
        "Name": "Molly Malone",
        "IDNumber": "8606125033087"
    },
    {
        "ID": 3,
        "Name": "Rianna Chetty",
        "IDNumber": "6207145122087"
    }

];
Linkz
  • 51
  • 1
  • 3
  • 9

2 Answers2

1

Initialize an ajax request to your json file and then it returns the data from the file. For example if "data" is what returned from the ajax request, then your array would be

var myList = JSON.parse(data);

Hope this helps you

AmGates
  • 2,127
  • 16
  • 29
1

using jQuery:

$.getJSON('JsonData.json')
    .done(function(data) {
      alert(data[0].ID);
    });
Adassko
  • 5,201
  • 20
  • 37
  • I tried the code below: $.getJSON('JsonData.json') .done(function (data) { var myList = data; }); And I get an error Error: 'myList' is undefined – Linkz Oct 25 '13 at 13:50
  • @ Linkz Could you please paste the whole code pls – AmGates Oct 26 '13 at 16:25