javascript - Uncaught SyntaxError: Unexpected token h error for json option passed as param to a function - Stack Overflow

JSON Array:var data= '[{"id":"1","text":"B.Sc"},{"id&

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
Add a ment  | 

2 Answers 2

Reset to default 1

Escape 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\")"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744773398a4592898.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信