-1
{
   "code":100,
   "data":{
      "month":[
         {
            "yearText":"2011",
            "months":[
               {
                  "monthText":"6",
                  "days":[
                     {
                        "dayText":"13",
                        "cios":[
                           {
                              "status":"continues",
                              "start":"23:00:00",
                              "end":"23:59:59",
                              "id":12
                           }
                        ],
                        "bois":[
                           {
                              "status":"continues",
                              "start":"23:30:00",
                              "end":"23:59:59",
                              "id":12
                           }
                        ]
                     },
                     {
                        "dayText":"14",
                        "cios":[
                           {
                              "status":"continued",
                              "start":"00:00:00",
                              "end":"01:00:00",
                              "id":12
                           },
                           {
                              "status":"within",
                              "start":"11:42:14",
                              "end":"11:43:45",
                              "id":11
                           }
                        ],
                        "bois":[
                           {
                              "status":"continued",
                              "start":"00:00:00",
                              "end":"00:30:00",
                              "id":12
                           },
                           {
                              "status":"within",
                              "start":"11:42:39",
                              "end":"11:43:33",
                              "id":11
                           }
                        ]
                     }
                  ]
               }
            ]
         }
      ],
      "next":"\/attendance\/get-history\/2011\/07",
      "previous":"\/attendance\/get-history\/2011\/05"
   },
   "msg":"Attendance history of John Doe on June, 2011."
}

I need to read that file which is "attendance.json" and save the data as variables using jQuery.

Thanks

hilarl
  • 6,570
  • 13
  • 48
  • 57

2 Answers2

1

I'm not entirely sure what you mean or why you've included the PHP tag but provided the attendance.json file is in the same domain as your JavaScript (Google "Same Origin Policy") ...

$.getJSON('attendance.json', function(data, textStatus, jqXHR) {
    // data contains your JSON object
    alert(data.code); // 100
});

For an external data source (separate domain), this is only possible if the remote server supports a JSONP request.

Otherwise, you could try fetching the data server-side (PHP)

$data = json_decode(file_get_contents('http://example.com/attendance.json'));

See http://api.jquery.com/jQuery.getJSON/ and http://php.net/manual/en/function.json-decode.php

Phil
  • 157,677
  • 23
  • 242
  • 245
0

jQuery has a getJSON method

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Can you say a bit more on this ? I tried the following code but it doesn't work ``` $.getJSON('data.json', function(data){return data}) ```` but this is not working. Essentially I'm trying to assign a variable to the contents of the JSON file to be used in other functions. Thanks. – J Verma Jul 01 '15 at 19:12
  • http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Quentin Jul 01 '15 at 19:15