JSON data from a file using jquery

I wrote jquery to get the data by assigning the variable a hard-coded value.

My requirement is to get the same data from a json file, can anyone help me with this code with this. Please find the following code:

$(function () {
  var jsonCalendarTreeStructure = [
    {
      text: 'Years',
      nodes: [
        {
          text: '2013',
          type: 'Y',
          nodes: [
            {
              text: '13-Q1',
              type: 'Q',
            },
            {
              text: '13-01',
              type: 'M',
            },
            {
              text: '13-02',
            },
            {
              text: '13-03',
            }
          ]
        }
      ]
    }
  ];
  $('#Dyanmic').treeview({
    data: jsonCalendarTreeStructure,
  });
}
+4
source share
2 answers
$.getJSON('URL to JSON file', function(data){
   //use data
});

You can use jquery getJSON.

0
source

Your data should be accessible in json in this format:

[  
   {  
      "text":"Years",
      "nodes":[  
         {  
            "text":"2013",
            "type":"Y",
            "nodes":[  
               {  
                  "text":"13-Q1",
                  "type":"Q"
               },
               {  
                  "text":"13-01",
                  "type":"M"
               },
               {  
                  "text":"13-02"
               },
               {  
                  "text":"13-03"
               }
            ]
         }
      ]
   }
]

Using any method (ajax, for example) you can deserialize it:

$.get('url', function (data) {
    $('#Dyanmic').treeview({
    data: data,
});
0
source

Source: https://habr.com/ru/post/1691186/


All Articles