Alternatives to JavaScript eval() for parsing JSON

前端 未结 9 1286
忘了有多久
忘了有多久 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:45

    The alternative to evaluating the code is to parse it manually. It's not as hard as it sounds but it's quite a lot heavier at runtime. You can read about it here.

    The important part to note is evaluating JSON is not inherently insecure. As long as you trust the source not to balls things up. That includes making sure that things passed into the JSON encoder are properly escaped (to stop people 2 steps up the stream executing code on your users' machines).

提交回复
热议问题