2017-12-18 2 views
0

私はtest.jsでこのJSON文字列を持っているファイル:Nodejsは文字列化されたJSONを解析しません。オンラインのバリデータは有効な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); 
} 

ノードは、私はプログラム上で実行すると、エラーを次のように私を与える:

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を作成するには、どれが正しいか、文字列内でどのような変更が必要ですか?

答えて

3

オンラインバリデータに渡す文字列が、渡す文字列と同じではありませんJSON.parse

JSONとJavaScriptは共通のエスケープ文字を共有します。

JavaScriptパーサは、テンプレート文字列リテラルを解析するときに消費します。

SQL statement \"insert into context 

JSONリテラル二重引用符を表すJavaScriptソースコードにおけるエスケープ二重引用符を含む:これは、(例えば)このセクションがあることを意味します。

リテラルの二重引用符は、JSONの文字列の途中では使用できません。二重引用符はJSON文字列を終了し、それに続くiは無効です。

JSONのエスケープ文字として表示されるように、JavaScriptソースコードの\をエスケープする必要があります。

SQL statement \\"insert into context 

JavaScriptプログラムで文字列リテラルでJSONを埋め込むための良い理由はほとんどありません。ほとんどの場合、JSONをリテラルJavaScriptシンタックスとして扱うほうがよいでしょう。

let a = { 
    "context1": [{ 
    // etc 
}; 
関連する問題