无法访问 JSON 中的特定字段,说 javascript 中未定义

Can't access to a specfic field in JSON, saying undefined in javascript

我在 Laravel + socket.io 有一个项目,我需要访问正在广播的 json 中的特定字段。这是代码。

socket.js

redis.on('message', function(channel, message) {
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event,message.data); 
});

在我的客户端套接字中,我有这个。

socket.on("comment-channel:App\Events\CommentEvent", function(message){
        alert(message.data);
 });

那么就成功了,提醒一下。

[{
    "id": 136,
    "content": "dffsadf",
    "user_id": "1",
    "task_id": "33",
    "created_at": "2016-03-29 10:47:19",
    "user": {
        "id": 1,
        "first_name": "Hulk",
        "last_name": "Hogan",
        "username": "",
        "email": " hulhogan@yahoo.com",
        "company_id": "1",
        "role_id": "0",
        "photo": "\/assets\/apps\/img\/photos\/lvrv5VOGRskwPHvFVakp.jpeg",
        "position": "asdfsadf",
        "phone": "+75843857834",
        "city": "",
        "country": "Singapore",
        "timezone": "",
        "created_at": "2016-03-10 04:16:24",
        "updated_at": "2016-03-10 07:54:12",
        "deleted_at": null
    }
}]

然后当我尝试这个时

alert(message.data.task_id);

alert(message.data['task_id']);

我得到'undefined'..

如何访问task_id?谢谢!!!!

message.data好像是个数组,试试alert(message.data[0].task_id);

编辑:如果它不起作用,则问题不存在...

var message = {
 data : [{
   "id" : 136,
   "content" : "dffsadf",
   "user_id" : "1",
   "task_id" : "33",
   "created_at" : "2016-03-29 10:47:19",
   "user" : {
    "id" : 1,
    "first_name" : "Hulk",
    "last_name" : "Hogan",
    "username" : "",
    "email" : " hulhogan@yahoo.com",
    "company_id" : "1",
    "role_id" : "0",
    "photo" : "\/assets\/apps\/img\/photos\/lvrv5VOGRskwPHvFVakp.jpeg",
    "position" : "asdfsadf",
    "phone" : "+75843857834",
    "city" : "",
    "country" : "Singapore",
    "timezone" : "",
    "created_at" : "2016-03-10 04:16:24",
    "updated_at" : "2016-03-10 07:54:12",
    "deleted_at" : null
   }
  }
 ]
};

alert(message.data[0].task_id);

编辑 2 您确定您的 message.data 没有被解析为字符串吗? 尝试转换为 json object

var message = {
 data : '[{\r\n' + 
'   "id" : 136,\r\n' + 
'   "content" : "dffsadf",\r\n' + 
'   "user_id" : "1",\r\n' + 
'   "task_id" : "33",\r\n' + 
'   "created_at" : "2016-03-29 10:47:19",\r\n' + 
'   "user" : {\r\n' + 
'    "id" : 1,\r\n' + 
'    "first_name" : "Hulk",\r\n' + 
'    "last_name" : "Hogan",\r\n' + 
'    "username" : "",\r\n' + 
'    "email" : " hulhogan@yahoo.com",\r\n' + 
'    "company_id" : "1",\r\n' + 
'    "role_id" : "0",\r\n' + 
'    "photo" : "\/assets\/apps\/img\/photos\/lvrv5VOGRskwPHvFVakp.jpeg",\r\n' + 
'    "position" : "asdfsadf",\r\n' + 
'    "phone" : "+75843857834",\r\n' + 
'    "city" : "",\r\n' + 
'    "country" : "Singapore",\r\n' + 
'    "timezone" : "",\r\n' + 
'    "created_at" : "2016-03-10 04:16:24",\r\n' + 
'    "updated_at" : "2016-03-10 07:54:12",\r\n' + 
'    "deleted_at" : null\r\n' + 
'   }\r\n' + 
'  }\r\n' + 
' ]'
};

var obj = JSON.parse(message.data);
alert(obj[0].task_id);

var data = [{
    "id": 136,
    "content": "dffsadf",
    "user_id": "1",
    "task_id": "33",
    "created_at": "2016-03-29 10:47:19",
    "user": {
        "id": 1,
        "first_name": "Hulk",
        "last_name": "Hogan",
        "username": "",
        "email": " hulhogan@yahoo.com",
        "company_id": "1",
        "role_id": "0",
        "photo": "\/assets\/apps\/img\/photos\/lvrv5VOGRskwPHvFVakp.jpeg",
        "position": "asdfsadf",
        "phone": "+75843857834",
        "city": "",
        "country": "Singapore",
        "timezone": "",
        "created_at": "2016-03-10 04:16:24",
        "updated_at": "2016-03-10 07:54:12",
        "deleted_at": null
    }
}]
  
  alert(data[0].task_id);

这不是对象而是对象数组,您需要先访问数组 json[0] 中的对象,然后才能访问 json 的任何键和值。

编辑:

阅读您的评论后,它的提示 '[' 我了解到它的 String 数据在使用前需要解析。

替换你的代码:

 socket.on("comment-channel:App\Events\CommentEvent", function(message){
message = JSON.parse(message);
            alert(message.data[0].task_id);
     });