【发布时间】:2018-03-15 23:45:55
【问题描述】:
以下是我收到的 json:
{
"total": 5,
"responses": [{
"gender": "Female",
"age": 66
}, {
"gender": "Male",
"age": 52
}]
}
以下是我用来接收和解析json的代码
// Declare a proxy to reference the hub.
$.connection.hub.url = 'https://www.url...';
var res = $.connection.resHub;
// Create a function that the hub can call to broadcast messages.
res.client.broadcastRes = function (resp) {
var now = new Date();
console.log(now.toLocaleTimeString(), 'signalR survey data received', JSON.parse(resp));
createChart(JSON.parse(resp.responses));
};
$.connection.hub.start();
在控制台中,我可以通过 console.log(JSON.parse(resp)); 看到上面显示的整个 JSON 响应
但我得到了错误
JSON 中位置 0 处的意外标记 u 在 JSON.parse() 处
当我使用resp.responses时
我哪里错了?
我们将不胜感激任何形式的帮助。谢谢。
【问题讨论】:
-
它已经是一个javascript对象,只是不要解析它
-
resp.responses是undefined- 因此意外的u作为undefined被字符串化。 -
请先搜索错误信息再询问。
标签: javascript json signalr