Eval() = Unexpected token : error

前端 未结 8 1785
囚心锁ツ
囚心锁ツ 2020-12-09 04:28

I tried this simple JavaScript code:

eval(\'{\"Topics\":[\"toto\",\"tata\",\"titi\"]}\')

In the Chrome console, for example, this returns

相关标签:
8条回答
  • 2020-12-09 05:00

    Because eval does not force an expression context and the string provided is an invalid JavaScript program, thus the first three tokens (and how they are looked at) are:

    {            // <-- beginning of a block, and NOT an Object literal
    "Topics"     // <-- string value, okay (note this is NOT a label)
    :            // <-- huh? expecting ";" or "}" or an operator, etc.
    

    Happy coding.

    0 讨论(0)
  • 2020-12-09 05:02

    if you are using JQuery use the function $.parseJSON(), worked for me, had the same problem

    0 讨论(0)
提交回复
热议问题