JSON.parse 期望 ',' 或 '}',有什么想法?两者都无效。

4

JSON.parse和eval函数都出现了错误。我使用http://jsonlint.com/验证过JSON格式,你有什么想法吗?

<script type="text/javascript"> 

var obj = JSON.parse('{ "defaults": { "recordtext": "View", "emptyrecords": "No records to view", "loadtext": "Loading...", "pgtext": "Page" }, "search": { "caption": "Search...", "Find": "Find", "Reset": "Reset", "odata": [ "equal", "not equal", "less", "less or equal", "greater", "greater or equal", "begins with", "does not begin with", "is in", "is not in", "ends with", "does not end with", "contains", "does not contain" ], "groupOps": [ { "op": "AND", "text": "all" }, { "op": "OR", "text": "any" } ], "matchText": " match", "rulesText": " rules" }, "edit": { "addCaption": "Add Record", "editCaption": "Edit Record", "bSubmit": "Submit", "bCancel": "Cancel", "bClose": "Close", "saveData": "Data has been changed! Save changes?", "bYes": "Yes", "bNo": "No", "bExit": "Cancel", "msg": { "required": "Field is required", "number": "Please, enter valid number", "minValue": "value must be greater than or equal to ", "maxValue": "value must be less than or equal to", "email": "is not a valid e-mail", "integer": "Please, enter valid integer value", "date": "Please, enter valid date value", "url": "is not a valid URL. Prefix required (\"http://\" or \"https://\")", "nodefined": " is not defined!", "novalue": " return value is required!", "customarray": "Custom function should return array!", "customfcheck": "Custom function should be present in case of custom checking!" } }, "view": { "caption": "View Record", "bClose": "Close" }, "del": { "caption": "Delete", "msg": "Delete selected record(s)?", "bSubmit": "Delete", "bCancel": "Cancel" }, "nav": { "edittext": "", "edittitle": "Edit selected row", "addtext": "", "addtitle": "Add new row", "deltext": "", "deltitle": "Delete selected row", "searchtext": "", "searchtitle": "Find records", "refreshtext": "", "refreshtitle": "Reload Grid", "alertcap": "Warning", "alerttext": "Please, select row", "viewtext": "", "viewtitle": "View selected row" }, "col": { "caption": "Select columns", "bSubmit": "Ok", "bCancel": "Cancel" }, "errors": { "errcap": "Error", "nourl": "No url is set", "norecords": "No records to process", "model": "Length of colNames <> colModel!" }, "formatter": { "integer": { "thousandsSeparator": " ", "defaultValue": "0" }, "number": { "decimalSeparator": ".", "thousandsSeparator": " ", "decimalPlaces": 2, "defaultValue": "0.00" }, "currency": { "decimalSeparator": ".", "thousandsSeparator": " ", "decimalPlaces": 2, "prefix": "", "suffix": "", "defaultValue": "0.00" }, "date": { "dayNames": [ "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], "monthNames": [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], "AmPm": [ "am", "pm", "AM", "PM" ], "srcformat": "Y-m-d", "newformat": "m/d/Y", "masks": { "ISO8601Long": "Y-m-d H:i:s", "ISO8601Short": "Y-m-d", "ShortDate": "n/j/Y", "LongDate": "l, F d, Y", "FullDateTime": "l, F d, Y g:i:s A", "MonthDay": "F d", "ShortTime": "g:i A", "LongTime": "g:i:s A", "SortableDateTime": "Y-m-d\\TH:i:s", "UniversalSortableDateTime": "Y-m-d H:i:sO", "YearMonth": "F, Y" }, "reformatAfterEdit": false }, "baseLinkUrl": "", "showAction": "", "target": "", "checkbox": { "disabled": true }, "idName": "id" }, "emptyrecords": "test" }');
var obj2 = eval ( '(' + '{ "defaults": { "recordtext": "View", "emptyrecords": "No records to view", "loadtext": "Loading...", "pgtext": "Page" }, "search": { "caption": "Search...", "Find": "Find", "Reset": "Reset", "odata": [ "equal", "not equal", "less", "less or equal", "greater", "greater or equal", "begins with", "does not begin with", "is in", "is not in", "ends with", "does not end with", "contains", "does not contain" ], "groupOps": [ { "op": "AND", "text": "all" }, { "op": "OR", "text": "any" } ], "matchText": " match", "rulesText": " rules" }, "edit": { "addCaption": "Add Record", "editCaption": "Edit Record", "bSubmit": "Submit", "bCancel": "Cancel", "bClose": "Close", "saveData": "Data has been changed! Save changes?", "bYes": "Yes", "bNo": "No", "bExit": "Cancel", "msg": { "required": "Field is required", "number": "Please, enter valid number", "minValue": "value must be greater than or equal to ", "maxValue": "value must be less than or equal to", "email": "is not a valid e-mail", "integer": "Please, enter valid integer value", "date": "Please, enter valid date value", "url": "is not a valid URL. Prefix required (\"http://\" or \"https://\")", "nodefined": " is not defined!", "novalue": " return value is required!", "customarray": "Custom function should return array!", "customfcheck": "Custom function should be present in case of custom checking!" } }, "view": { "caption": "View Record", "bClose": "Close" }, "del": { "caption": "Delete", "msg": "Delete selected record(s)?", "bSubmit": "Delete", "bCancel": "Cancel" }, "nav": { "edittext": "", "edittitle": "Edit selected row", "addtext": "", "addtitle": "Add new row", "deltext": "", "deltitle": "Delete selected row", "searchtext": "", "searchtitle": "Find records", "refreshtext": "", "refreshtitle": "Reload Grid", "alertcap": "Warning", "alerttext": "Please, select row", "viewtext": "", "viewtitle": "View selected row" }, "col": { "caption": "Select columns", "bSubmit": "Ok", "bCancel": "Cancel" }, "errors": { "errcap": "Error", "nourl": "No url is set", "norecords": "No records to process", "model": "Length of colNames <> colModel!" }, "formatter": { "integer": { "thousandsSeparator": " ", "defaultValue": "0" }, "number": { "decimalSeparator": ".", "thousandsSeparator": " ", "decimalPlaces": 2, "defaultValue": "0.00" }, "currency": { "decimalSeparator": ".", "thousandsSeparator": " ", "decimalPlaces": 2, "prefix": "", "suffix": "", "defaultValue": "0.00" }, "date": { "dayNames": [ "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], "monthNames": [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], "AmPm": [ "am", "pm", "AM", "PM" ], "srcformat": "Y-m-d", "newformat": "m/d/Y", "masks": { "ISO8601Long": "Y-m-d H:i:s", "ISO8601Short": "Y-m-d", "ShortDate": "n/j/Y", "LongDate": "l, F d, Y", "FullDateTime": "l, F d, Y g:i:s A", "MonthDay": "F d", "ShortTime": "g:i A", "LongTime": "g:i:s A", "SortableDateTime": "Y-m-d\\TH:i:s", "UniversalSortableDateTime": "Y-m-d H:i:sO", "YearMonth": "F, Y" }, "reformatAfterEdit": false }, "baseLinkUrl": "", "showAction": "", "target": "", "checkbox": { "disabled": true }, "idName": "id" }, "emptyrecords": "test" }' + ')');

</script>  

我认为你需要转义冒号。 - David G
@pst 我使用jsonlint进行了检查。它显示为“有效JSON”。我也在我的问题中提及了这一点。 - user1596914
@David 我没在哪里看到?这是IE文档的相关内容 http://msdn.microsoft.com/zh-cn/library/cc836466%28v=VS.94%29.aspx? - user1596914
@user1596914 这个问题出现在哪些浏览器上?可以创建一个 jsfiddle 测试案例吗? - user166390
2个回答

4

你没有正确转义引号,需要再加一个反斜杠;

这是错误的;

JSON.parse('{ "fails" : "\"http://\" or \"https://\"" }')

应该是这样的:

JSON.parse('{ "passes" : "\\"http://\\" or \\"https://\\"" }')

通常情况下,对于如此庞大的JSON结构,尝试将其分解成较小的块并测试它们,直到您隔离出引起问题的部分。 - Dave Anderson

2

当JSON数据有效时,它基本上意味着您可以编写以下内容:

var xx = <that-thing-you-validated>;

然而,JSON.parse()需要一个JSON格式的字符串,这是不同的。请注意:
var yy = {text: "hello \"world\""};
console.log(JSON.stringify(yy));
// output: "{"text":"hello \"world\""}"

好的,现在我们将其复制/粘贴,并用单引号替换外部双引号(因为我们不想转义内部的双引号)。

console.log('{"text":"hello \"world\""}');
// output: {"text":"hello "world""}

哇,反斜杠怎么了?!在JavaScript中创建包含反斜杠的字符串文字时,也需要对其进行转义。

console.log('{"text":"hello \\"world\\""}');
// output: {"text":"hello \"world\""}

现在可以正常工作了吗?
JSON.parse('{"text":"hello \\"world\\""}');
// output: Object

是的! :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接