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

本文标签: