我的test.js文件中有这个JSON字符串:
let a=JSON.parse(`{
"context1": [{
"ID": 4,
"CONTEXT": "bye",
"CREATED_TIME": "2017-12-17 03:56:53.761",
"LAST_UPDATED_TIME": "2017-12-17 03:56:53.761"
}],
"context_INSERT": {
"error": "StatementCallback; bad SQL grammar [insert into context(context, created_time, last_updated_time) values('someone',2017-12-16 09:49:00.09','2017-12-16 09:49:00.09')]; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement \"insert into context(context, created_time, last_updated_time) values('someone',2017-12-16 09:49:00.09','2017-12-16 09:49:00.09[*]')\"; SQL statement:\ninsert into context(context, created_time, last_updated_time) values('someone',2017-12-16 09:49:00.09','2017-12-16 09:49:00.09') [42000-196]\n\tat org.h2.message.DbException.getJdbcSQLException(DbException.java:345)\n\tat org.h2.message.DbException.get(DbException.java:179)\n\tat org.h2.message.DbException.get(DbException.java:155)\n\tat org.h2.message.DbException.getSyntaxError(DbException.java:191)\n\tat org.h2.command.Parser.getSyntaxError(Parser.java:534)\n\tat org.h2.command.Parser.checkRunOver(Parser.java:3766)\n\tat org.h2.command.Parser.initialize(Parser.java:3677)\n\tat org.h2.command.Parser.parse(Parser.java:308)\n\tat org.h2.command.Parser.parse(Parser.java:297)\n\tat org.h2.command.Parser.prepareCommand(Parser.java:258)\n\tat org.h2.engine.Session.prepareLocal(Session.java:578)\n\tat org.h2.server.TcpServerThread.process(TcpServerThread.java:264)\n\tat org.h2.server.TcpServerThread.run(TcpServerThread.java:158)\n\tat java.lang.Thread.run(Unknown Source)\n"
},
"contexts": [{
"ID": 3,
"CONTEXT": "greetings",
"CREATED_TIME": "2017-12-16 09:49:00.09",
"LAST_UPDATED_TIME": "2017-12-16 09:49:00.09"
}, {
"ID": 4,
"CONTEXT": "bye",
"CREATED_TIME": "2017-12-17 03:56:53.761",
"LAST_UPDATED_TIME": "2017-12-17 03:56:53.761"
}]
}`);
console.log('done');
if(a.context_INSERT.error){
console.log(a.context_INSERT.error);
}
当我运行上面的程序时,Node给出了以下错误:
undefined:9
"error": "StatementCallback; bad SQL grammar [insert into context(context, created_time, last_updated_time) v
alues('someone',2017-12-16 09:49:00.09','2017-12-16 09:49:00.09')]; nested exception is org.h2.jdbc.JdbcSQLException: Syntax
error in SQL statement "insert into context(context, created_time, last_updated_time) values('someone',2017-12-16 09:49:00.09
','2017-12-16 09:49:00.09[*]')"; SQL statement:
^
SyntaxError: Unexpected token i in JSON at position 429
at JSON.parse (<anonymous>)
at Object.<anonymous> (C:\Users\IBM_ADMIN\Desktop\temp\test1\test.js:1:74)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:598:3
但是在线JSON解析器如:https://jsonlint.com/正在传递,字符串作为有效的JSON?
哪一个是正确的,我需要在我的字符串中进行哪些更改才能使其成为节点的有效json?
您传递给在线验证器的字符串与您传递给JSON.parse
的字符串不同!
JSON和JavaScript共享一组常见的转义字符。
解析模板字符串文字时,JavaScript解析器将消耗掉。这意味着本节(例如):
SQL statement \"insert into context
包含JavaScript源代码中的转义双引号,表示JSON中的文字双引号。
JSON中的字符串中间不允许使用文字双引号。双引号结束JSON字符串,因此跟随它的i
无效。
您需要在JavaScript源代码中转义\
,以便它在JSON中显示为转义字符。
SQL statement \\"insert into context
几乎没有充分的理由在JavaScript程序中将JSON嵌入到字符串文字中。将JSON视为文字JavaScript语法几乎总是更好。
let a = {
"context1": [{
// etc
};