Tuesday, July 10, 2012

JSON and the Null Character

I've been working with WebSockets lately and have been attempting at porting them across various applications.  Well, everything has been fine and dandy, until I bothered to get to the HTML5 version of our application (or rather, the future of it).

For some reason, I kept getting "invalid_token" or "invalid token" with no trace of any other information.

Turns out, C# and the WebSocket were appending \0 at the end of the data.  D'oh!

The simple fix for this (when you're having issues with JSON.parse) is to change your code from:

var jsonObject = JSON.parse(jsonEncodedString);


var jsonObject = JSON.parse(jsonEncodedString.replace(/\0/g, ""));

With that, your code should hopefully work!  If not, there's always http://jsonlint.com/ to attempt to validate your JSON.

No comments:

Post a Comment