可以't访问JSON中的特定字段,在javascript中表示未定义

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

本文关键字:字段 未定义 表示 javascript JSON 访问 可以      更新时间:2023-09-26

我在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?非常感谢。

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

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