簡體   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