如何使用空格访问json对象键

时间:2014-05-13 17:58:40

标签: javascript jquery json

我有一个json文件,几乎每个对象的键都有一个空格,用于它们的名字和姓氏。因此,每当我尝试调用该键的值时,它都会显示为“未定义”。如何获取要显示的值?

以下是我的json文件

的示例
{"Wojciech Szczesny":"yes","Lukasz Fabianski":"no","Emiliano Viviano":"no","Olivier Giroud":"yes","Per Mertesacker":"yes","Bacary Sagna":"yes","Laurent Koscielny":"no","Santi Cazorla":"yes","Mikel Arteta":"yes","Mesut \u00d6zil":"no","Kieran Gibbs":"yes","Aaron Ramsey":"no","Jack Wilshere":"no","Mathieu Flamini":"yes","Tomas Rosicky":"yes","Lukas Podolski":"yes","Nacho Monreal":"no","Theo Walcott":"no","Thomas Vermaelen":"yes","Carl Jenkinson":"no","Alex Oxlade-Chamberlain":"no","Serge Gnabry":"no","Kim Kallstrom":"no","Nicklas Bendtner":"no","Abou Diaby":"no","Park Chu-Young":"no","Emmanuel Frimpong":"no","Yaya Sanogo":"no","Ryo Miyaichi":"no","Hector Bellerin":"no","Chuba Akpom":"no","Isaac Hayden":"no","Gideon Zelalem":"no"}

这是我的代码

$.ajax({
url:'ars.json',
dataType:'json',
cache: false,
success: function(data) {
var count = 0;
arrayTesting.push(Object.getOwnPropertyNames(data[0]));
for(var key in data) {
            //This line does not run at all
    if(data[key].Per Mertesacker){
        count++;
    }else{}

}
    //Nothing prints out in the console
    console.log(data[key].Per Mertesacker);
}
});

1 个答案:

答案 0 :(得分:15)

更改

console.log(data[key].Per Mertesacker);

console.log(data[key]['Per Mertesacker']);

阅读Working with objects