admin管理员组文章数量:1392007
JSON Array:
var data= '[{"id":"1","text":"B.Sc"},{"id":"2","text":"M.Pharm"},{"id":"3","text":"M.Tech"},{"id":"4","text":"BBM"},{"id":"6","text":"MCA"},{"id":"10","text":"PGDMA"},{"id":"11","text":"Diploma"},{"id":"12","text":"Plus Two"},{"id":"14","text":"fdf"},{"id":"15","text":"dfdf"},{"id":"16","text":"alert("hi");"},{"id":"1235","text":"B.Tech"},{"id":"10001","text":"MBA"}]';
selectParams['data'] = jQuery.parseJSON(data); // errors happens here
I tried in / and its throwing error as well.
The problem is with ("") double quotes within the JSON array.
I tried several options but nothing works. How can we parse JSON array with this structure like having double quotes and single quotes as values.
Any help is appreciated
JSON Array:
var data= '[{"id":"1","text":"B.Sc"},{"id":"2","text":"M.Pharm"},{"id":"3","text":"M.Tech"},{"id":"4","text":"BBM"},{"id":"6","text":"MCA"},{"id":"10","text":"PGDMA"},{"id":"11","text":"Diploma"},{"id":"12","text":"Plus Two"},{"id":"14","text":"fdf"},{"id":"15","text":"dfdf"},{"id":"16","text":"alert("hi");"},{"id":"1235","text":"B.Tech"},{"id":"10001","text":"MBA"}]';
selectParams['data'] = jQuery.parseJSON(data); // errors happens here
I tried in http://www.jsoneditoronline/ and its throwing error as well.
The problem is with ("") double quotes within the JSON array.
I tried several options but nothing works. How can we parse JSON array with this structure like having double quotes and single quotes as values.
Any help is appreciated
Share Improve this question asked Sep 1, 2014 at 10:58 WolverineWolverine 4753 gold badges8 silver badges27 bronze badges 1- Where did the JSON e from? All JSON libraries should do this right, don't try to create JSON by hand. – Barmar Commented Sep 1, 2014 at 11:03
2 Answers
Reset to default 1Escape your inner double quotes:
alert(\"hi\")
When generating JSON string, escape all keys and values
val.replace('"', '\"'); //json_encode($fullArray); in PHP will escape automatically.
so that "alert("hi");"
bees "alert(\"hi\")"
本文标签:
版权声明:本文标题:javascript - Uncaught SyntaxError: Unexpected token h error for json option passed as param to a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744773398a2624478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论