JSON Eval

Next ❯Reference

It's a javascript method used to evaluate a string and executes it as if it was a javascript code

  • Well, you can use it to store any javascript code into a JSON string

Think, Your JSON is having a function in it and Since, JSON doesn't store a function so, it must be in string
{"name":"Riya","marks":"function (m1,m2,m3){return (m1+m2+m3)/3;}"}

Now, To convert your string function back to real functional function, you have to use
eval()

Example:

a

var jsonObj='{"name":"Riya","marks":"function (m1,m2,m3){return (m1+m2+m3)/3;}"}';
var obj=JSON.parse(jsonObj);
//Converting string function into real function
obj.marks=eval("("+obj.marks+")");
$("p").html("Your Name : "+obj.name +
      "<br/>Your average mark : "+obj.marks(60,82,80));
Evalsubject
Evalclose
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var jsonObj='{"name":"Riya","marks":"function (m1,m2,m3){return (m1+m2+m3)/3;}"}';
var obj=JSON.parse(jsonObj);
//Converting string function into real function
obj.marks=eval("("+obj.marks+")");
$("p").html("Your Name : "+obj.name +
      "<br>Your average mark : "+obj.marks(60,82,80));
</script>
</body>
</html>

  • Reference
❮ Prev jQuery Ajax
Next ❯Reference
TryOut Examples"Learn to Explore..!"

TryOut Editor

receipt