1

当我打印这个:console.log( response );

它给了我这个 json 数组:

{    
    "myStudentId":17,
    "myName":"beng",
    "myAge":38,
    "myAddress":"Los Angeles, California, USA.",
    "myGender":"Female",
    "myReligion":"Catholic",
    "myStatus":"boom",
    "myOccupation":"Actress",
    "myEducationLevel":"Undefined",
    "graduate":true
}

我想访问该graduate值。到目前为止我尝试的是:

console.log( response["graduate"] );
console.log( response.graduate );

但这一切都回来了undefined。有没有办法做到这一点?如果可能的话,我不想使用循环。如果可能的话,我想通过密钥访问它。

编辑:

我正在使用x-editable 成功方法进行回调。

$(this).editable({
    pk: pk,
    name: id,
    url: '../admin/edit_studentInfo.do',
    validate: function( value ) {
       if($.trim( value ) == '') {
          return 'This field is required';
       } else if( isNaN( value ) && id == 'age' ) {
          return 'Only accepts numbers';
       }
        },
    success: function(response, newValue) { // call back
       var test = response;
       console.log( JSON.stringify(response) );
       console.log( response );
       console.log( response["graduate"] );
       console.log( response["myName"] + ', ' + response.myName );
    }
});

我包括了我的可编辑初始化和测试

4

2 回答 2

3

response是一个字符串。您需要先将其解析为 JSON,然后才能访问编码对象的任何属性:

response = JSON.parse(response);
console.log(response.graduate);

或者,告诉使用$.getJSON或通过dataType : "JSON",jQuery 将为您进行解析。

于 2013-12-29T03:33:56.713 回答
0

添加一个 ajaxOption

ajaxOptions: {
dataType: 'json'
}
于 2013-12-29T03:33:45.660 回答