2017-07-10 58 views
0

我使用jint解析JavaScript代码的对象,下面的代码JS工作:评估和演示的javascript使用jint

  • 1
  • [1]
  • {}

但是这一次失败:

{ a: 1}

与此错误:

int.Parser.ParserException: Line 1: Unexpected token : at Jint.Parser.JavaScriptParser.ThrowError(Token token, String messageFormat, Object[] arguments) at Jint.Parser.JavaScriptParser.ThrowUnexpected(Token token) at Jint.Parser.JavaScriptParser.ConsumeSemicolon() at Jint.Parser.JavaScriptParser.ParseStatement() at Jint.Parser.JavaScriptParser.ParseStatement() at Jint.Parser.JavaScriptParser.ParseSourceElement() at Jint.Parser.JavaScriptParser.ParseStatementList() at Jint.Parser.JavaScriptParser.ParseBlock() at Jint.Parser.JavaScriptParser.ParseStatement() at Jint.Parser.JavaScriptParser.ParseSourceElement() at Jint.Parser.JavaScriptParser.ParseSourceElements() at Jint.Parser.JavaScriptParser.ParseProgram() at Jint.Parser.JavaScriptParser.Parse(String code, ParserOptions options) at Jint.Engine.Execute(String source)

我不想反序列化JSON文件,我想执行一个JavaScript对象,我想有这样的:

{ 
    id: 'one', 
    code: function() { console.log('hello'); } 
} 

我注意到,如果我这样做:

var x = {a: 1} 
x 

然后它的工作,但我需要它在我的方案中的JavaScript对象。

有没有办法做到这一点?

+0

它可能将其解释为块而不是对象字面量。试着用括号括起来,即'({a:1})' –

+0

它的工作原理!谢谢! –

+0

你可以发布你的答案,以便我可以接受吗? –

回答

1

{ a: 1}被解释为块语句而不是对象字面量。解决办法是用圆括号包装它:

({ a: 1 })