使用jQuery从文件中获取JSON数据

Ara*_*nan 5 html javascript treeview jquery

我编写了一个jquery,通过将硬编码值分配给变量来获取数据。

我的要求是从json文件中获取相同的数据,任何人都可以通过此代码来帮助我。请在下面找到代码:

$(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,
  });
}
Run Code Online (Sandbox Code Playgroud)

Tre*_*vor 0

$.getJSON('URL to JSON file', function(data){
   //use data
});
Run Code Online (Sandbox Code Playgroud)

您可以使用 jquery getJSON。