Javascript 使用 Jquery 从本地文件中获取 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2792423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:56:31  来源:igfitidea点击:

Using Jquery to get JSON objects from local file

javascriptjqueryjsonlocal-files

提问by ThoughtCrhyme

I'm trying to get a list of JSON objects (products) from a local file using Jquery and store all the objects in a single array called allItems. The file is co-located in the same directory as the code, and it's called "allItems.json". Here's how I'm doing it now:

我正在尝试使用 Jquery 从本地文件中获取 JSON 对象(产品)列表,并将所有对象存储在名为 allItems 的单个数组中。该文件与代码位于同一目录中,名为“allItems.json”。这是我现在的做法:

function getAllSupportedItems(){
    var allItems = new Array();
    $.getJSON("allItems.json",
         function(data){
             $.each(data.items, 
             function(item){
                 allItems.push(item);
             });
         });
    return allItems;
}

Based on this example: http://api.jquery.com/jQuery.getJSON/

基于这个例子:http: //api.jquery.com/jQuery.getJSON/

回答by Ates Goral

For getAllSupportedItemsto be able to return any items, the AJAX call needs to run synchronously.

为了getAllSupportedItems能够返回任何项目,AJAX 调用需要同步运行。

getJSONtranslates to the following asynchronous call:

getJSON转换为以下异步调用:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

Asynchronous is the default. You therefore need to explicitly change your request to a synchronous one:

异步是默认设置。因此,您需要明确地将您的请求更改为同步请求:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback,
  async: false
});

An alternative is to rethink the way you use getAllSupportedItemsand make it into an asynchronous utility:

另一种方法是重新考虑您的使用方式getAllSupportedItems并将其变成一个异步实用程序:

function getAllSupportedItems(callback){
    $.getJSON("allItems.json",
         function(data){
             var allItems = [];
             $.each(data.items, 
             function(item){
                 allItems.push(item);
             });
             callback(allItems);
             // callback(data.items); should also work
         });
}


Update

更新

When I initially wrote this answer, jQuery didn't have built-in Deferred support. It is a lot more concise and flexible to do something like this today:

当我最初写这个答案时,jQuery 没有内置的延迟支持。今天做这样的事情更加简洁和灵活:

function getAllSupportedItems( ) {
    return $.getJSON("allItems.json").then(function (data) {
        return data.items;
    });
}

// Usage:
getAllSupportedItems().done(function (items) {
    // you have your items here
});

回答by Pointy

How are you using this? If you're expecting the main function ("getAllSupportedItems") to returnthe array you make, well that won't work. The $.getJSONfunction is asynchronous, and so the handler won't actually build the array until after the outer function has returned.

你怎么用这个?如果您希望主函数 ("getAllSupportedItems")返回您创建的数组,那么这将不起作用。该$.getJSON函数是异步的,因此在外部函数返回之前,处理程序实际上不会构建数组。