简体   繁体   中英

Can I execute script from json file

Can I execute script from json file? For example:

helloworld.json
{
"script" : "console.log('helloworld');"
}

I want to run the script in the json file in my html or js file.

Point eval to the string to execute it as code

var json = {"script":"console.log('helloworld');"}
eval(json["script"])

Also see Execute JavaScript code stored as a string

Yes, you have to:

  1. Load your json file
  2. Execute the script using eval()

Whether that's a good idea or not though depends on your specific use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM