javascript 语法错误:意外数字 (JSON.parse)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13889386/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:04:16  来源:igfitidea点击:

SyntaxError: Unexpected Number (JSON.parse)

javascriptjsonparsing

提问by Niks Jain

My JSON string,

我的 JSON 字符串,

JSON.parse('{"start_date_time": ["2012-12-05 04:45:42.135000", "None"], "terminal_no": ["T1081", "None"], "master_doc_no": ["100008", "100008"], "notes": ["", ""], "doc_no": ["1000018", "1000019"], "location_code": ["1005", "1005"], "end_date_time": ["2012-12-05 05:27:04.529000", "None"], "doc_status": ["CC Ended", "Draft"], "bc_list": ["[{\"465\":\"85\"},{\"306\":\"6\"},{\"306\":\"47\"},{\"306\":\"366\"},{\"306\":\"634\"}]", "[{\"257\":\"14\"}]"]}')

But its throwing SyntaxError: Unexpected Number

但它抛出SyntaxError: Unexpected Number

Where am i wrong over here?

我哪里错了?

回答by Brad

You can start by simplifying this down to where the problem occurs, in bc_list...

您可以首先将其简化为问题发生的位置,在bc_list...

JSON.parse('{"bc_list": ["", "{\"257\":\"14\"}]"]}')

The issue is that your backslashes are being considered for the outer quotes on JSON.parse()instead of the inner data. You must escape the backslashes as well.

问题是您的反斜杠正在考虑用于外部引号JSON.parse()而不是内部数据。您还必须避开反斜杠。

JSON.parse('{"bc_list": ["", "{\"257\":\"14\"}]"]}')

Your whole line fixed becomes:

您的整行固定变为:

JSON.parse('{"start_date_time": ["2012-12-05 04:45:42.135000", "None"], "terminal_no": ["T1081", "None"], "master_doc_no": ["100008", "100008"], "notes": ["", ""], "doc_no": ["1000018", "1000019"], "location_code": ["1005", "1005"], "end_date_time": ["2012-12-05 05:27:04.529000", "None"], "doc_status": ["CC Ended", "Draft"], "bc_list": ["[{\"465\":\"85\"},{\"306\":\"6\"},{\"306\":\"47\"},{\"306\":\"366\"},{\"306\":\"634\"}]", "[{\"257\":\"14\"}]"]}')

Don't use JSON data within strings within JSON data. It's a mess.

不要在 JSON 数据的字符串中使用 JSON 数据。一团糟。

回答by Mr L

This normally means your you are missing an operator, or have an illegal operator, in a calculation.

这通常意味着您在计算中缺少运算符或使用非法运算符。

For example:

例如:

var a = 1000 * 1000; // correct
var b = 1000 1000;   // incorrect
var c = 1234;        // correct
var d = 1,234;       // incorrect

vars b and d will result in:

vars b 和 d 将导致:

Uncaught SyntaxError: Unexpected number