Skip to content
Loading
creating a json file and display them
Here we want to dispaly the employee name from our json file:
get the element from the body,
  1. var mainContainer = document.getElementById("empData"); 
Next step is to create a simple loop. We can then get every object in our list of JSON object and append it into our main div. Here after you need to create a new div element and fill  dive by name. After you append to the main container.
 function appendData(data) {    
  1.   var mainContainer = document.getElementById("empData");    
  2.   for (var i = 0; i < data.length; i++) {    
  3.     var div = document.createElement("div");    
  4.     div.innerHTML = 'EmployeeName: ' + data[i].firstName + ' ' + data[i].lastName;    
  5.     mainContainer.appendChild(div);    
  6.   }    
  7. }   
After you can run the index file it displays the all employees name.
  • I need a seperate json file for the below attached html page.
  • @sachin singh I need using javascript not jquery
  • Sachin Singh
    You can do it like this
    suppose your json file is something as below (array of json object)
    1. [{"id":1,"name":"sachin"},{"id":2,"name":"Singh"}];  
     then you can read it in table as
    1. function getArray(){  
    2.     return $.getJSON('yourFileName.json');  
    3. }  
    1. var data=getArray();  
    2. $(data).each(function(i, item)  
    3. {  
    4.  var tr=document.createElement("tr");  
    5. $(tr).append(""+(item.id+"").append(""+ item.name+"");  
    6.  $("#tbl").append(tr);  
    7. })  
    html
    1. "tbl">