0 votes
228 views
asked in java script by
recategorized by

Hi

   how to parse Json in javascript? How to parse the follwoing content in java scripe

{
  "employees": [
    {
      "firstName": "John",
      "lastName": "Doe"
    },
    {
      "firstName": "Anna",
      "lastName": "Smith"
    },
    {
      "firstName": "Peter",
      "lastName": "Jones"
    }
  ]
}

1 Answer

0 votes
answered by (8.2k points)

Hi check this code

 

  <!DOCTYPE html>

<html>
<body>
 
<h2>Create Object from JSON String</h2>
 
<p id="demo"></p>
 
<script>
var text = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';
 
obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
</script>
 
</body>
</html>

Categories

...