Javascript eval fails on complicated json array -
this question has answer here:
- json javascript escape 4 answers
i want convert json string object eval, fails error like:
uncaught syntaxerror: unexpected identifier vm250:1
below string: '[{"quiz_id":"3","_id":"1","option_in_json":"[{\"option\":\"1\",\"is_answer\":false},{\"option\":\"2\",\"is_answer\":true}]","question":"1+1"}]';
seems there wrong in bold part, don't know how fix code below not working
var m='[{"quiz_id":"3","_id":"1","option_in_json":"[{\"option\":\"1\",\"is_answer\":false},{\"option\":\"2\",\"is_answer\":true}]","question":"1+1"}]'; eval(m);
the code below working think data structure of json string ok
var m=[{"quiz_id":"3","_id":"1","option_in_json":"[{\"option\":\"1\",\"is_answer\":false},{\"option\":\"2\",\"is_answer\":true}]","question":"1+1"}]; alert(m[0].option_in_json);
also tried $.parsejson no luck
it not work because not escaping data inside string literal correctly. @ value of m
in first case, quotation marks:
[{"option_in_json":"[{"option":"1","is_answer":false}]","question":"1+1"}] // ^ ^
i removed irrelevant data. should able see cannot valid javascript (or json), because quotation mark before option
terminates string.
in order put data inside string literal, should either fix data doesn't contain nested json, or escape \
:
'[{"option_in_json":"[{\\"option\\": ... }]"}]'
better of course if not putting in string literal in first place.
var m='[{"quiz_id":"3","_id":"1","option_in_json": [{"option":"1","is_answer":false},{"option":"2","is_answer":true}],"question":"1+1"}]'; // ^-- don't wrap in "" no need escape inner double quotes. console.dir(json.parse(m));
Comments
Post a Comment