繁体   English   中英

使用jint评估javascript对象

[英]Eval javascript object using jint

我正在使用jint解析javascript代码,以下js代码有效:

  • 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对象。

有没有办法做到这一点?

{ a: 1}被解释为一个块语句,而不是对象文字。 解决方案是将其包装在括号中:

({ a: 1 })

暂无
暂无

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

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