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.
delete r
supposed to do?delete obj.prop
removes theprop
property ofobj
, it does not work with variables. Moreover, you're overwriting it right after that line, get rid of that delete!JSON.parse()
? And for backward compatibility o JSON.parse you can use github.com/douglascrockford/JSON-js