简体   繁体   English

Node.js没有解析字符串化的JSON,在线验证者将其视为有效的JSON?

[英]Nodejs not parsing a stringified JSON, where as online validator are considering it as valid JSON?

I have this JSON string in my test.js file: 我的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 gives me following error when I run above program: 当我运行上述程序时,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

However online JSON parser like : https://jsonlint.com/ is passing, the string as a valid JSON ? 但是,在线JSON解析器(例如: https//jsonlint.com/)正在传递,该字符串作为有效JSON?

Which one is correct and what change I need to do in my string to make it valid json for node? 哪一个是正确的,我需要对字符串进行什么更改以使其对节点有效为json?

The string you are passing to the online validator is not the same as the string you are passing to JSON.parse ! 您传递给在线验证器的字符串与您传递给JSON.parse的字符串不同!

JSON and JavaScript share a common set of escape characters. JSON和JavaScript共享一组通用的转义字符。

The JavaScript parser will consume then when parsing your template string literal. JavaScript解析器将在解析模板字符串文字时使用。 This means that this section (for example): 这意味着该部分(例如):

 SQL statement \\"insert into context 

includes an escaped double quote in JavaScript source code which represents a literal double quote in JSON. 在JavaScript源代码中包含转义的双引号,表示JSON中的文字双引号。

Literal double quotes are not allowed in the middle of strings in JSON. JSON中的字符串中间不允许使用文字双引号。 The double quote ends the JSON string and the i that follows it is therefore invalid. 双引号以JSON字符串结尾,因此其后的i无效。

You need to escape the \\ in the JavaScript source code so that it appears as an escape character in the JSON. 您需要对JavaScript源代码中的\\进行转义,以使其在JSON中显示为转义字符。

SQL statement \\"insert into context

There is almost never a good reason to embed JSON in a string literal in a JavaScript program. 几乎没有一个很好的理由将JSON嵌入到JavaScript程序的字符串文字中。 It is almost always better to just treat the JSON as literal JavaScript syntax. 将JSON视为原义JavaScript语法几乎总是更好。

let a = {
    "context1": [{
    // etc
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM