最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论