0

我在 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']);

我得到'未定义'..

我怎样才能访问task_id?谢谢!!!!

4

2 回答 2

3

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 对象

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);

于 2016-03-29T14:36:58.587 回答
0

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);
     });
于 2016-03-29T14:43:54.343 回答