Reputation: 645
I have a string returned from the server. It is of the format,
[{"text":"content"}]
where content
is a dynamic string and will vary based on user actions. In one such particular case, the returned string cannot be eval'ed, but when I use JSON.parse
, it works fine without issues. My eval is like below
eval("(" + response + ")").
Due to certain restrictions I cannot paste the problematic string here. But are there any particular cases where eval
will not work and JSON.parse
will work?
EDIT1 : I narrowed down the problem and the particular string which gives the problem is,
[{"con" :"g
<\/font><\/td"}]
This string cannot be eval'ed but can by parsed. I guess the issue is because of the sting"/". But I am not able to understand why. Any help in this regard will be great.
EDIT2: There exists an invisible character between g and < and that is causing the issue. Does anyone know what character it is? I will be changing the eval to JSON.parse. But I wanted to know the reason why it failed.
Upvotes: 1
Views: 946
Reputation: 664548
That buggy character is U+8232, the Unicode LINE SEPARATOR
. It leads to "unterminated string literal" syntax errors in various browsers, this is why it does not work to eval()
the string. JSON.parse
can work around that, as JSON is not really a JS subset in that perspective.
Upvotes: 2
Reputation: 399
You are confusing parsing JSON and parsing JavaScript. eval()
does not parse JSON.
Eval would require valid JS syntax.
In "test":"test"
, the quotes around the property are not valid in javascript object literal notation
Upvotes: -1