4

我的 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\"}]"]}')

但它抛出SyntaxError: Unexpected Number

我在这里错在哪里?

4

2 回答 2

5

You can start by simplifying this down to where the problem occurs, in 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('{"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.

于 2012-12-15T05:06:38.250 回答
1

这通常意味着您在计算中缺少一个运算符,或者有一个非法运算符。

例如:

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

vars b 和 d 将导致:

Uncaught SyntaxError: Unexpected number
于 2018-10-22T16:05:43.183 回答