可以't访问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']);
我得到了"未定义"。。
如何访问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);
});
相关文章:
- 主干窗体隐藏字段未呈现
- Kendo UI网格复选框列字段未定义
- 按enter键时,jQuery UI自动完成字段未提交
- 输入字段未使用 ng-keydown 事件更新
- Javascript空字段未正确验证
- 日期-时间范围指令字段未显示任何值
- 如何使操作仅在字段未选中时运行
- 输入字段未更新
- 使用下划线 js map 函数时,Mongo 引用字段未实例化
- SuiteCRM内联编辑的字段未保存
- Javascript 新对象字段未保存
- 防止在文本字段输入中键入,即使字段未禁用/只读
- “金额”字段未传递到PayPal
- 如果字段未在 ng 重复中填写,如何禁用按钮
- 选择字段未在生成时加载
- 禁用表单提交按钮,直到必填字段未填充为止
- 为什么某些Facebook Graph API事件字段未定义
- Angularjs中隐藏的输入字段未更新
- Ext JS 4复选框字段未显示
- responseText字段未定义