尝试将一些静态数据发送到websocket并收到相同的内容。数据是
var details=
[
{
"_id": "5799fac61ee480492224071a",
"index": 0,
"age": 25,
"name": "Jean Pierce",
"gender": "female",
"email": "jeanpierce@cincyr.com"
},
{
"_id": "5799fac678aae9a71af63ef2",
"index": 1,
"age": 23,
"name": "Laurie Lopez",
"gender": "female",
"email": "laurielopez@cincyr.com"
},
{
"_id": "5799fac6c237d929693c08d7",
"index": 2,
"age": 21,
"name": "Padilla Barrett",
"gender": "male",
"email": "padillabarrett@cincyr.com"
}
];
当收到上述数据时,它采用字符串形式,因此我使用JSON.parse(data)
将其解析为JSON并将其存储在本地变量中。现在,这里的问题是,对于解析,需要将其转换为[Object,Object]
形式而不是控制台上的[object Object],[object Object]
。怎么做到这一点?
更新:相同的代码如下,请检查。
使用以下代码将数据发送到websocket服务器
if (client.readyState === client.OPEN) {
client.send(JSON.stringify(details));
}
在接收端,数据收到 -
client.onmessage = function(e) {
console.log(" Fetched data :"+JSON.parse(e.data));
this.setState({ val: JSON.parse(e.data)
});
}.bind(this);
第二个语句中的console.log()
将结果显示为[object Object],[object Object]
,但需要在反应[ Object Object]
中解析相同内容。怎么做到这一点?
答案 0 :(得分:-1)
你走了:
console.log('[' + array.fill('Object').toString() + ']')
测试:
var array = [
{
"_id": "5799fac61ee480492224071a",
"index": 0,
"age": 25,
"name": "Jean Pierce",
"gender": "female",
"email": "jeanpierce@cincyr.com"
},
{
"_id": "5799fac678aae9a71af63ef2",
"index": 1,
"age": 23,
"name": "Laurie Lopez",
"gender": "female",
"email": "laurielopez@cincyr.com"
},
{
"_id": "5799fac6c237d929693c08d7",
"index": 2,
"age": 21,
"name": "Padilla Barrett",
"gender": "male",
"email": "padillabarrett@cincyr.com"
}
]
console.log('[' + array.fill('Object').toString() + ']')
虽然我老实说不知道你为什么要这个而不是另一个 - 两者都同样(不)有帮助。您确定不想要console.dir
吗?