我使用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对象。
有办法做到这一点吗?
发布于 2017-07-10 15:46:42
{ a: 1}被解释为块语句,而不是对象文本。解决方案是将其括在括号中:
({ a: 1 })https://stackoverflow.com/questions/45016307
复制相似问题