javascript 未捕获的语法错误:json 选项的意外标记 h 错误作为参数传递给函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25604320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught SyntaxError: Unexpected token h error for json option passed as param to a function
提问by Wolverine
JSON Array:
JSON 数组:
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.org/and its throwing error as well.
我在http://www.jsoneditoronline.org/和它的抛出错误中也尝试过。
The problem is with ("") double quotes within the JSON array.
问题在于 JSON 数组中的 ("") 双引号。
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.
我尝试了几种选择,但没有任何效果。我们如何使用这种结构解析 JSON 数组,例如将双引号和单引号作为值。
Any help is appreciated
任何帮助表示赞赏
回答by Justinas
When generating JSON string, escape all keys and values
生成 JSON 字符串时,转义所有键和值
val.replace('"', '\"'); //json_encode($fullArray); in PHP will escape automatically.
so that "alert("hi");"
becomes "alert(\"hi\")"
这样"alert("hi");"
就变成"alert(\"hi\")"
回答by Andy
Escape your inner double quotes:
转义你内心的双引号:
alert(\"hi\")