简体   繁体   English

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

[英]JSON data from file using jquery

I have written a jquery to get data by assigning a hard coded value to variable. 我编写了一个jquery来通过为变量赋值硬编码来获取数据。

My requirement is to get the same data from a json file, can anyone help me with this code in doing that. 我的要求是从json文件中获取相同的数据,任何人都可以帮助我使用此代码。 Please find the code below: 请找到以下代码:

$(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,
  });
}
$.getJSON('URL to JSON file', function(data){
   //use data
});

You can use jquery getJSON. 您可以使用jquery getJSON。

Your data should be available in json in this format: 您的数据应以json格式提供:

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

With whatever method (ajax, for example), you can then deserialise it: 使用任何方法(例如ajax),然后可以反序列化它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM