Alternatives to JavaScript eval() for parsing JSON

前端 未结 9 1288
忘了有多久
忘了有多久 2020-12-01 08:23

Quick Question. Eval in JavaScript is unsafe is it not? I have a JSON object as a string and I need to turn it into an actual object so I can obtain the data:



        
9条回答
  •  攒了一身酷
    2020-12-01 08:47

    Unsafe? That depends on if you can trust the data.

    If you can trust that the string will be JSON (and won't include, for example, functions) then it is safe.

    That said - if you are using jQuery, why are you doing this manually? Use the dataType option to specify that it is JSON and let the library take care of it for you.

提交回复
热议问题