5 April 2014

Reading JSON values Example 1

<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<p>
First Name: <span id="fname"></span><br>
Last Name: <span id="lname"></span><br>
</p>
<script>
var txt = '{"employees":[' +
'{"firstName":"Krish","lastName":"Ram" },' +
'{"firstName":"John","lastName":"Smith" },' +
'{"firstName":"Subramani","lastName":"Chandran" }]}';

var obj = eval ("(" + txt + ")");

document.getElementById("fname").innerHTML=obj.employees[1].firstName
document.getElementById("lname").innerHTML=obj.employees[1].lastName
</script>
</body>
</html>

No comments:

Post a Comment