首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jint的Eval javascript对象

使用jint的Eval javascript对象
EN

Stack Overflow用户
提问于 2017-07-10 15:41:45
回答 1查看 974关注 0票数 0

我使用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对象,我希望有这样的东西:

代码语言:javascript
复制
{
  id: 'one',
  code: function() { console.log('hello'); }
}

我注意到如果我这么做

代码语言:javascript
复制
var x = {a: 1}
x

然后它就能工作了,但我需要它成为我的场景中的javascript对象。

有办法做到这一点吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-10 15:46:42

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

代码语言:javascript
复制
({ a: 1 })
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45016307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档