简体   繁体   English

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

[英]jquery plugin not converting JSON string correctly

I'm getting a JSON string through a comet request. 我通过彗星请求得到一个JSON字符串。 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":"ben+team2@squadedit.com","lastPing":1329409537308,"sessionLeader":true,"cursorPosition":{"line":0,"ch":0}}}"

I run it through the jquery json plugin: 我通过jquery json插件运行它:

var r = $.evalJSON(jsonstring);

But it will not convert the "changeData" object correctly. 但是,它将无法正确转换“ changeData”对象。 The rest of it all works, but changedData.from.line and changedData.to.line both result in NaN. 其余所有工作正常,但是changedData.from.line和changedData.to.line均导致NaN。

I've also tried sending changedData.from.line as a string and then using 我也尝试过将changeData.from.line作为字符串发送,然后使用

Number(changedData.from.line)

to convert it back to a number, but it still returns NaN. 将其转换回数字,但仍返回NaN。 I'm almost positive that 20 is a number, but I've been wrong before. 我几乎肯定20是一个数字,但之前我做错了。

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. 其中“ frame.body”是上面发布的不带引号的字符串。

Error is somewhere else. 错误在其他地方。 Using parseJSON works perfectly with the string you've given http://jsfiddle.net/mendesjuan/qG6Qv/1/ 使用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":"ben+team2@squadedit.com","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) . 可能是您的问题是将parseJSON的返回值分配给变量r ,然后尝试执行Number(changedData.from.line) There is no variable called changedData 没有一个名为changedData变量

前导引号和尾随引号"无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM