当尝试从返回的ajax中将json值存储为变量时...

$.ajax({
url:'example.php',
type:'POST',
dataType: 'json',
success:function(data){

    var checkname = data.name;  // working
    var check1m = data.1m;      // Here I get the SyntaxError

}
});


...,并且其列名以数字(例如1m)开头:

[{"name":"Peter","city":"London","1m":"not attending","2m":"attending"}]


我收到以下错误:

SyntaxError: identifier starts immediately after numeric literal


如何将值存储在变量(check1m)中?

最佳答案

您可以使用offset访问值。

 var check1m = data['1m'];





var json = [{"name":"Peter","city":"London","1m":"not attending","2m":"attending"}] ;
console.log(json[0]['1m']);

10-06 03:41