javascript 使用 json.js 对多维数组进行字符串化的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5168400/
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-10-25 16:13:20  来源:igfitidea点击:

Issues Stringifying a multidimensional array with json.js

javascriptjqueryjson

提问by Coronier

I have problems with .stringify(), but I think my JavaScript array must be wrong, here's my code:

我有问题.stringify(),但我认为我的 JavaScript 数组一定是错误的,这是我的代码:

var questions = new Array();

$('#Valid').hover(function(){
    for (i=0;i < $('.Questions').length;i++){
        questions[i]=new Array();
        questions[i]['numero']=$('.Numero:eq('+i+')').html();
        questions[i]['question']=$('.ItemInput:eq('+i+')').val();
        questions[i]['variable']=$('.VarName:eq('+i+')').val();
    }

    var stringJSON=JSON.stringify(questions)
    alert (stringJSON)
})

The stringJSON var returns :

stringJSON var 返回:

[[]]

What am I doing wrong?

我究竟做错了什么?

回答by Lightness Races in Orbit

Arrays have integer keys, not strings.

数组具有整数键,而不是字符串。

Use an object instead; objects in JS sort of look like associative arrays:

改用一个对象;JS 中的对象有点像关联数组:

var questions = new Array();
$('#Valid').hover(function(){
    for (var i=0;i < $('.Questions').length;i++){
        questions[i]={};
        questions[i]['numero']=$('.Numero:eq('+i+')').html();
        questions[i]['question']=$('.ItemInput:eq('+i+')').val();
        questions[i]['variable']=$('.VarName:eq('+i+')').val();
    }

    var stringJSON=JSON.stringify(questions);
    alert(stringJSON);
});

Setting questions[i]to {}is the key.

设置questions[i]{}关键。

You can shorten this syntax:

您可以缩短此语法:

var questions = new Array();
$('#Valid').hover(function(){
    for (var i=0;i < $('.Questions').length;i++){
        questions[i] = {
            numero:   $('.Numero:eq('+i+')').html(),
            question: $('.ItemInput:eq('+i+')').val(),
            variable: $('.VarName:eq('+i+')').val()
        };
    }

    var stringJSON=JSON.stringify(questions);
    alert(stringJSON);
});

回答by ThiefMaster

Fix:

使固定:

Replace questions[i]=new Array();with questions[i] = {};(or = new Object();if you really want this "ugly" syntax).

替换questions[i]=new Array();questions[i] = {};(或者= new Object();如果您真的想要这种“丑陋”的语法)。

Explanation:

解释:

Arrays in JavaScript are like arrays in languages like C: They only support integer indexes in the interval [0, array.length).

JavaScript 中的数组类似于 C 等语言中的数组:它们只支持区间内的整数索引[0, array.length)

For associative arrays, you use objects which are created using the object literal {}.

对于关联数组,您可以使用使用对象字面量创建的对象{}

回答by Pointy

First, your "questions" variable appears to be intended as a realarray. What you're putting insidethe array, however, are notreal arrays — they're just objects with properties.

首先,您的“问题”变量似乎是一个真正的数组。你把什么内部的阵列,但是,是不是真正的阵列-他们只是用对象的属性。

var questions = [];

$('#Valid').hover(function(){
    for (var i=0;i < $('.Questions').length;i++){
        questions[i] = {
          'numero': $('.Numero:eq('+i+')').html(),
          'question': $('.ItemInput:eq('+i+')').val(),
          'variable': $('.VarName:eq('+i+')').val()
        };
    }

    var stringJSON=JSON.stringify(questions)
    alert (stringJSON)
});

(Note that I also added a varfor the "i" loop variable - important!)

(请注意,我还var为“i”循环变量添加了一个- 很重要!)

Now, as to why the thing is coming up empty, well, I suspect it's because $('.Questions')is coming up empty.

现在,至于为什么这件事是空的,好吧,我怀疑是因为它$('.Questions')是空的。

Oh and also, this is something you could use the jQuery ".map()" API for:

哦,还有,您可以使用 jQuery“.map()”API 来实现以下目的:

 $('#Valid').hover(function() {
   questions = $('.Questions').map(function(i, q) {
     return {
       'numero': $('.Numero:eq('+i+')').html(),
       'question': $('.ItemInput:eq('+i+')').val(),
       'variable': $('.VarName:eq('+i+')').val()
     };
   }).get();

   var stringJSON = JSON.stringify(questions);
   alert(stringJSON);
});

That's a little nicer because it gets around the ugly problem of re-evaluating $('.Questions')on every iteration of the loop.

这更好一点,因为它解决了$('.Questions')在循环的每次迭代中重新评估的丑陋问题。