I'm retrieving some stringifyed JSON via TCP in node.js an want to parse it. So my approach is similiar to this. I shortend and simplified it, so you don't have to know the surrounding logic.
socket.on(data, function(data) {
console.log(data.toString()); // Shows the original stringifyed version
console.log(JSON.parse(data.toString())); // Doesn't work
});
The complete (beautified) JSON is this. As you can see, there are no errors.
{
result: success,
source: chat,
success: {
message: test,
time: 1331770513,
player: silvinci
}
}
But JSON.parse(data.toString())
always throws me this dumb error:
{result:success,source:console,success:{time:1331762264,line:20
^
SyntaxError: Unexpected token {
at Object.parse (native)
at Socket.<anonymous> (/home/node/api.js:152:35) // irrelevant from here on
at Socket.emit (events.js:67:17)
at TCP.onread (net.js:347:14)
So I thougt: What could be wrong with the JSON-String. Let's try it directly. Should not work. Surprise, Surprise! It worked. Why does it work when I directly enter the String?