jquery 插件无法正确转换 JSON 字符串

发布于 2025-01-06 11:25:37 字数 1408 浏览 2 评论 0原文

我通过 comet 请求获取 JSON 字符串。该字符串如下:

"{"tab":2,"changedData":{"from":{"line":20,"ch":0},"to":{"line":20,"ch":0},"text":["a"]},"cmd":"copyChunk","timestamp":1329409543902,"person":{"comradeID":"4ef37369b4812","firstName":"","lastName":"","fullName":"ben team2","nickName":"ben t.","messageCount":0,"email":"[email protected]","lastPing":1329409537308,"sessionLeader":true,"cursorPosition":{"line":0,"ch":0}}}"

我通过 jquery json 插件运行它:

var r = $.evalJSON(jsonstring);

但它不会正确转换“changeData”对象。其余部分都有效,但changedData.from.line和changedData.to.line都导致NaN。

我还尝试将changedData.from.line作为字符串发送,然后使用

Number(changedData.from.line)

将其转换回数字,但它仍然返回NaN。我几乎确信 20 是一个数字,但我以前也曾错过。

提前致谢。

更新:

我很抱歉,开头和结尾的引号实际上不是字符串的一部分。复制/粘贴的陷阱。

这是上下文中的代码:

onMessage : function(frame)
{
    //This function calls the handlers.
    //this is fired every time we recieve a message from orbited
    //this is what calls the handler functions
    //body is a json string containing whatever data was sent via the send() function

    delete r;
    //convert the body to an object
    var r = $.evalJSON(frame.body);

其中“frame.body”是上面发布的不带引号的字符串。

I'm getting a JSON string through a comet request. The string is as follows:

"{"tab":2,"changedData":{"from":{"line":20,"ch":0},"to":{"line":20,"ch":0},"text":["a"]},"cmd":"copyChunk","timestamp":1329409543902,"person":{"comradeID":"4ef37369b4812","firstName":"","lastName":"","fullName":"ben team2","nickName":"ben t.","messageCount":0,"email":"[email protected]","lastPing":1329409537308,"sessionLeader":true,"cursorPosition":{"line":0,"ch":0}}}"

I run it through the jquery json plugin:

var r = $.evalJSON(jsonstring);

But it will not convert the "changeData" object correctly. The rest of it all works, but changedData.from.line and changedData.to.line both result in NaN.

I've also tried sending changedData.from.line as a string and then using

Number(changedData.from.line)

to convert it back to a number, but it still returns NaN. I'm almost positive that 20 is a number, but I've been wrong before.

Thanks in advance.

Update:

I apologize, the quotes at the beginning and end aren't actually part of the string. The pitfalls of copy/paste.

Here is the code in context:

onMessage : function(frame)
{
    //This function calls the handlers.
    //this is fired every time we recieve a message from orbited
    //this is what calls the handler functions
    //body is a json string containing whatever data was sent via the send() function

    delete r;
    //convert the body to an object
    var r = $.evalJSON(frame.body);

where "frame.body" is the string posted above without quotes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此生挚爱伱 2025-01-13 11:25:37

错误在其他地方。使用 parseJSON 与您提供的字符串完美配合 http://jsfiddle。 net/mendesjuan/qG6Qv/1/

var str = '{"tab":2,"changedData":{"from":{"line":20,"ch":0},"to":{"line":20,"ch":0},"text":["a"]},"cmd":"copyChunk","timestamp":1329409543902,"person":{"comradeID":"4ef37369b4812","firstName":"","lastName":"","fullName":"ben team2","nickName":"ben t.","messageCount":0,"email":"[email protected]","lastPing":1329409537308,"sessionLeader":true,"cursorPosition":{"line":0,"ch":0}}}';

var json = $.parseJSON(str);
alert(json.changedData.from.line); //outputs 20

您的问题可能是您将 parseJSON 的返回值分配给变量 r 然后您尝试执行以下操作数字(changedData.from.line)。没有名为 changedData 的变量

Error is somewhere else. Using parseJSON works perfectly with the string you've given http://jsfiddle.net/mendesjuan/qG6Qv/1/

var str = '{"tab":2,"changedData":{"from":{"line":20,"ch":0},"to":{"line":20,"ch":0},"text":["a"]},"cmd":"copyChunk","timestamp":1329409543902,"person":{"comradeID":"4ef37369b4812","firstName":"","lastName":"","fullName":"ben team2","nickName":"ben t.","messageCount":0,"email":"[email protected]","lastPing":1329409537308,"sessionLeader":true,"cursorPosition":{"line":0,"ch":0}}}';

var json = $.parseJSON(str);
alert(json.changedData.from.line); //outputs 20

It may be that your problem is that you're assigning the return of parseJSON to a variable r and then you're trying to do Number(changedData.from.line). There is no variable called changedData

做个ˇ局外人 2025-01-13 11:25:37

前导和尾随引号 " 无效。

The leading and trailing quote marks " are invalid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文