解析函数

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>把字符串转换为函数</h1>
  6.  
  7. <p id="demo"></p>
  8.  
  9. <script>
  10. var text = '{"name":"Bill Gates", "age":"function() {return 62;}", "city":"Seattle"}';
  11. var obj = JSON.parse(text);
  12. obj.age = eval("(" + obj.age + ")");
  13. document.getElementById("demo").innerHTML = obj.name + ", " + obj.age();
  14. </script>
  15.  
  16. </body>
  17. </html>