如何使用javascript变量访问JSON对象

时间:2022-12-01 16:31:38

What I mean by that is say I have JSON data as such:

我的意思是说我有JSON数据:

[{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}]

and I want to do something like this:

我想做这样的事情:

var x = "ADAM";
alert(data.x.TEST);

4 个解决方案

#1


20  

var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}],
    x = "ADAM";

alert(data[0][x].TEST);

http://jsfiddle.net/n0nick/UWR9y/

http://jsfiddle.net/n0nick/UWR9y/

#2


4  

Since objects in javascripts are handled just like hashmaps (or associative arrays) you can just do data['adam'].TEST just like you could do data.adam.TEST. If you have a variable index, just go with the [] notation.

由于javascripts中的对象就像hashmaps(或关联数组)一样处理,你可以只做数据['adam']。就像你可以做data.adam.TEST一样。如果你有一个变量索引,只需使用[]表示法。

var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}]
alert(data[0].ADAM.TEST);
alert(data[0]['ADAM'].TEST)

if you just do

如果你这样做

var data = {"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}};

you could access it using data.ADAM.TEST and data['ADAM'].TEST

你可以使用data.ADAM.TEST和data ['ADAM']来访问它.TEST

#3


0  

That won't work as you're setting x to be a string object, no accessing the value from your array:

这不会起作用,因为您将x设置为字符串对象,而不是从数组中访问值:

alert(data[0]["ADAM"].TEST);

#4


0  

 var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}],
 x = "ADAM";

alert(data[x].TEST);

This is what worked for me. This way you can pass in a variable to the function and avoid repeating you code.

这对我有用。这样,您可以将变量传递给函数,避免重复代码。

function yourFunction(varName, elementName){
//json GET code setup
document.getElementById(elementName).innerHTML = data[varName].key1 + " " + data.[varName].key2;
}

#1


20  

var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}],
    x = "ADAM";

alert(data[0][x].TEST);

http://jsfiddle.net/n0nick/UWR9y/

http://jsfiddle.net/n0nick/UWR9y/

#2


4  

Since objects in javascripts are handled just like hashmaps (or associative arrays) you can just do data['adam'].TEST just like you could do data.adam.TEST. If you have a variable index, just go with the [] notation.

由于javascripts中的对象就像hashmaps(或关联数组)一样处理,你可以只做数据['adam']。就像你可以做data.adam.TEST一样。如果你有一个变量索引,只需使用[]表示法。

var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}]
alert(data[0].ADAM.TEST);
alert(data[0]['ADAM'].TEST)

if you just do

如果你这样做

var data = {"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}};

you could access it using data.ADAM.TEST and data['ADAM'].TEST

你可以使用data.ADAM.TEST和data ['ADAM']来访问它.TEST

#3


0  

That won't work as you're setting x to be a string object, no accessing the value from your array:

这不会起作用,因为您将x设置为字符串对象,而不是从数组中访问值:

alert(data[0]["ADAM"].TEST);

#4


0  

 var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}],
 x = "ADAM";

alert(data[x].TEST);

This is what worked for me. This way you can pass in a variable to the function and avoid repeating you code.

这对我有用。这样,您可以将变量传递给函数,避免重复代码。

function yourFunction(varName, elementName){
//json GET code setup
document.getElementById(elementName).innerHTML = data[varName].key1 + " " + data.[varName].key2;
}