Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从外部json文件中获取对象数组,并将其存储为javascript中的数组_Javascript_Jquery_Json - Fatal编程技术网

从外部json文件中获取对象数组,并将其存储为javascript中的数组

从外部json文件中获取对象数组,并将其存储为javascript中的数组,javascript,jquery,json,Javascript,Jquery,Json,如何将javascript数组分配给外部json文件中的对象数组 这是我试过的 JavaScript代码片段 var i = 0; var testjson = $.getJSON('/TestJSON'); jsonObj = JSON.parse(testjson); $("#testJSONBtn").click(function () { while (i <= jsonObj.events.length) { $("#JSONOutput").appen

如何将javascript数组分配给外部json文件中的对象数组

这是我试过的

JavaScript代码片段

var i = 0;
var testjson = $.getJSON('/TestJSON');
jsonObj = JSON.parse(testjson);

$("#testJSONBtn").click(function () {
    while (i <= jsonObj.events.length) {
        $("#JSONOutput").append(jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>")
        i += 1;
    }
});
我做错了什么?

你的问题是:

var testjson = $.getJSON('/TestJSON');
jsonObj = JSON.parse(testjson);
$.getJSON
已经将JSON解析为JavaScript对象,并将其传递给回调函数

改用这个:

$.getJSON('/TestJSON', function (jsonObj) {
    $("#testJSONBtn").click(function () {
        $.each(jsonObj.events, function (){
             $("#JSONOutput").append(this.title + ", " + this.date + ", " + this.explanation + "<br/>");
        });
    });
});
$.getJSON('/TestJSON',函数(jsonObj){
$(“#testJSONBtn”)。单击(函数(){
$.each(jsonObj.events,函数(){
$(“#JSONOutput”).append(this.title+”、“+this.date+”、“+this.explauration+”
); }); }); });

<强> P.S.性能,考虑缓存选择器,并一举将其全部追加。

< P>您的问题在这里:

var testjson = $.getJSON('/TestJSON');
jsonObj = JSON.parse(testjson);
$.getJSON
已经将JSON解析为JavaScript对象,并将其传递给回调函数

改用这个:

$.getJSON('/TestJSON', function (jsonObj) {
    $("#testJSONBtn").click(function () {
        $.each(jsonObj.events, function (){
             $("#JSONOutput").append(this.title + ", " + this.date + ", " + this.explanation + "<br/>");
        });
    });
});
$.getJSON('/TestJSON',函数(jsonObj){
$(“#testJSONBtn”)。单击(函数(){
$.each(jsonObj.events,函数(){
$(“#JSONOutput”).append(this.title+”、“+this.date+”、“+this.explauration+”
); }); }); });

P> >强> P.S.性能,考虑缓存选择器,并一举将其追加。<> Ajax函数<强>没有数据返回值< /强>,它们只返回Ajax对象。

您需要使用回调

试试这个:

$.getJSON('/TestJSON', function(jsonObj){
    $("#testJSONBtn").click(function () {
        for(var i = 0; i < jsonObj.events.length; ++i) {
            $("#JSONOutput").append(jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>")
        }
    });
});
$.getJSON('/TestJSON',函数(jsonObj){
$(“#testJSONBtn”)。单击(函数(){
对于(var i=0;i)
}
});
});
更好:

var btn = $("#testJSONBtn"); //cache the element
var output = $("#JSONOutput"); // ^^^
$.getJSON('/TestJSON', function(jsonObj){
    btn.click(function () {
        var val = "";
        for(var i = 0; i < jsonObj.events.length; ++i) {
            val += jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>";
        }
        output.append(val);
    });
});
var btn=$(“#testJSONBtn”)//缓存元素
变量输出=$(“#JSONOutput”);//^^^
$.getJSON('/TestJSON',函数(jsonObj){
点击(函数(){
var val=“”;
对于(var i=0;i”;
}
输出追加(val);
});
});

侧重点:


我不知道这是否是故意的,但在你的OP中JSON文件看起来不合法,你缺少逗号。()

AJAX函数没有数据返回值,它们只返回AJAX对象

您需要使用回调

试试这个:

$.getJSON('/TestJSON', function(jsonObj){
    $("#testJSONBtn").click(function () {
        for(var i = 0; i < jsonObj.events.length; ++i) {
            $("#JSONOutput").append(jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>")
        }
    });
});
$.getJSON('/TestJSON',函数(jsonObj){
$(“#testJSONBtn”)。单击(函数(){
对于(var i=0;i)
}
});
});
更好:

var btn = $("#testJSONBtn"); //cache the element
var output = $("#JSONOutput"); // ^^^
$.getJSON('/TestJSON', function(jsonObj){
    btn.click(function () {
        var val = "";
        for(var i = 0; i < jsonObj.events.length; ++i) {
            val += jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>";
        }
        output.append(val);
    });
});
var btn=$(“#testJSONBtn”)//缓存元素
变量输出=$(“#JSONOutput”);//^^^
$.getJSON('/TestJSON',函数(jsonObj){
点击(函数(){
var val=“”;
对于(var i=0;i”;
}
输出追加(val);
});
});

侧重点:


我不知道这是否是故意的,但在你的OP中JSON文件看起来不合法,你缺少逗号。()

您问题的标题说明您希望“从外部json文件获取一个对象数组,并将其存储为javascript中的数组”,因此我建议的解决方案包括将数据存储在数组中

var i;
// Prepare an empty array to store the results
var array = [];
// $.getJSON() is a wrapper for $.ajax(), and it returns a deffered jQuery object
var deferred = $.getJSON('/TestJSON');

deferred.done(function (response) {
    // Any code placed here will be executed if the $.getJSON() method
    // was completed successfully.
    for ( i = 0 ; i < response.length ; i++ ) {
        array.push({ 
            title: response.title, 
            date: response.date,
            explanation: response.explanation
        });
    }
});
vari;
//准备一个空数组来存储结果
var数组=[];
//$.getJSON()是$.ajax()的包装器,它返回一个不同的jQuery对象
var deferred=$.getJSON('/TestJSON');
延迟。完成(功能(响应){
//如果$.getJSON()方法
//已成功完成。
对于(i=0;i

您可以了解有关函数返回值以及对象使用的更多信息。

问题的标题说明您希望“从外部json文件获取一个对象数组,并将其存储为javascript中的数组”,因此我建议的解决方案包括将数据存储在数组中

var i;
// Prepare an empty array to store the results
var array = [];
// $.getJSON() is a wrapper for $.ajax(), and it returns a deffered jQuery object
var deferred = $.getJSON('/TestJSON');

deferred.done(function (response) {
    // Any code placed here will be executed if the $.getJSON() method
    // was completed successfully.
    for ( i = 0 ; i < response.length ; i++ ) {
        array.push({ 
            title: response.title, 
            date: response.date,
            explanation: response.explanation
        });
    }
});
vari;
//准备一个空数组来存储结果
var数组=[];
//$.getJSON()是$.ajax()的包装器,它返回一个不同的jQuery对象
var deferred=$.getJSON('/TestJSON');
延迟。完成(功能(响应){
//如果$.getJSON()方法
//已成功完成。
对于(i=0;i

您可以了解有关函数返回值以及使用对象的更多信息。

对于记录,
$。getJSON
是一个ajax调用。。。。它是异步的,不返回JSON。您必须在成功手册中使用它。您现在是否阅读了文档:?为什么要删除?@VoidKing:Ajax就是这样工作的。从外部URL接收数据是一个非阻塞过程。一旦接收到数据,就会通过调用提供的回调来通知代码。它与JSON本身无关。想象一下,你派助手去商店买东西。你只是站着等他回来吗?“我想你继续工作,直到他回来,然后处理他捡到的任何东西。”国王说。请花些时间复习一下。是的