在json数组中使用变量作为标识符

时间:2023-02-01 17:06:11

I'm wondering if it is possible to use assigned variables as identifier in a json array. When I tried this, I was getting some unexpected results:

我想知道是否可以在json数组中使用指定的变量作为标识符。当我尝试这个的时候,我得到了一些意想不到的结果:

(Code is simplified, parameters are passed in a different way)

(代码简化,参数以不同的方式传递)


var parameter = 'animal';
var value = 'pony';

Util.urlAppendParameters (url, {parameter : value});


Util.urlAppendParameters = function(url, parameters) {
    for (var x in parameters) {
        alert(x);
    }
}

Now the alert popup says: 'parameter' instead of 'animal'. I know I could use a different method (creating an array and assigning every parameter on a new line), but I want to keep my code compact.

现在弹出的警告是:“参数”而不是“动物”。我知道我可以使用不同的方法(创建一个数组并在新行上分配每个参数),但是我想保持代码的紧凑性。

So my question is: Is it possible to use a variable as an identifier in the json array, and if so, could you please tell me how?

我的问题是:是否可以在json数组中使用变量作为标识符,如果可以,请告诉我如何使用?

Thanks in advance!

提前谢谢!

3 个解决方案

#1


3  

No, you can't use a variable as an identifier within an object literal like that. The parser is expecting a name there so you can't do much else but provide a string. Similarly you couldn't do something like this:

不,在这样的对象文本中不能使用变量作为标识符。解析器期望在那里有一个名称,所以除了提供一个字符串之外,您不能做其他任何事情。同样,你也不能做这样的事情:

var parameter = 'animal';
var parameter = 'value'; //<- Parser expects a name, nothing more, so original parameter will not be used as name

The only work around if you really really want to use an object literal on a single line is to use eval:

如果你真的想在一行中使用对象文字,唯一的办法就是使用eval:

Util.urlAppendParameters (url, eval("({" + parameter + " : value})");

#2


10  

You will need to build your object in two steps, and use the [] property accessor:

您将需要在两个步骤中构建对象,并使用[]属性访问器:

var parameter = 'animal';
var value = 'pony';

var obj = {};
obj[parameter] = value;

Util.urlAppendParameters (url, obj);

I don't think JSON Array is the more correct term, I would call it Object literal.

我不认为JSON数组是更正确的术语,我把它叫做Object literal。

#3


1  

Depending on your needs you could also build your object with a helper function;

根据您的需要,您还可以使用helper函数构建对象;

Util.createParameters = function(args) {
    var O = {};
    for (var i = 0; i < arguments.length; i += 2)
        O[arguments[i]] = arguments[i + 1];
    return O
}

Util.urlAppendParameters (url, Util.createParameters(parameter, value, "p2", "v2"));

#1


3  

No, you can't use a variable as an identifier within an object literal like that. The parser is expecting a name there so you can't do much else but provide a string. Similarly you couldn't do something like this:

不,在这样的对象文本中不能使用变量作为标识符。解析器期望在那里有一个名称,所以除了提供一个字符串之外,您不能做其他任何事情。同样,你也不能做这样的事情:

var parameter = 'animal';
var parameter = 'value'; //<- Parser expects a name, nothing more, so original parameter will not be used as name

The only work around if you really really want to use an object literal on a single line is to use eval:

如果你真的想在一行中使用对象文字,唯一的办法就是使用eval:

Util.urlAppendParameters (url, eval("({" + parameter + " : value})");

#2


10  

You will need to build your object in two steps, and use the [] property accessor:

您将需要在两个步骤中构建对象,并使用[]属性访问器:

var parameter = 'animal';
var value = 'pony';

var obj = {};
obj[parameter] = value;

Util.urlAppendParameters (url, obj);

I don't think JSON Array is the more correct term, I would call it Object literal.

我不认为JSON数组是更正确的术语,我把它叫做Object literal。

#3


1  

Depending on your needs you could also build your object with a helper function;

根据您的需要,您还可以使用helper函数构建对象;

Util.createParameters = function(args) {
    var O = {};
    for (var i = 0; i < arguments.length; i += 2)
        O[arguments[i]] = arguments[i + 1];
    return O
}

Util.urlAppendParameters (url, Util.createParameters(parameter, value, "p2", "v2"));