Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. var txt = '{"employees":[' +
  2. '{"firstName":"John","lastName":"Doe","time":"9:15am","email":"john_doe@gmail.com" },' +
  3. '{"firstName":"Anna","lastName":"Smith","time":"9:15am","email":"anna@gmail.com" },' +
  4. '{"firstName":"Peter","lastName":"Jones" ,"time":"9:15am","email":"peter@gmail.com"}]}';
  5.  
  6. <div class="info">
  7. <ul>
  8. <li id="name"></li>
  9. <li id="time"></li>
  10. <li id="email"></li>
  11. </ul>
  12. </div>
  13.  
  14. <!DOCTYPE html>
  15. <html>
  16. <body>
  17. <h2>Create Object from JSON String</h2>
  18. <div id="output">
  19.  
  20. </div>
  21. <script type="text/javascript">
  22. var txt = '{"employees":[' +
  23. '{"firstName":"John","lastName":"Doe","time":"9:15am","email":"john_doe@gmail.com" },' +
  24. '{"firstName":"Anna","lastName":"Smith","time":"9:15am","email":"anna@gmail.com" },' +
  25. '{"firstName":"Peter","lastName":"Jones" ,"time":"9:15am","email":"peter@gmail.com"}]}';
  26.  
  27. var employees=JSON.parse(txt).employees;
  28. var container=document.getElementById("output");
  29.  
  30. for (i=0;i<employees.length;i++) { //Loops for the length of the list
  31. var info=document.createElement('div');
  32. info.className='info'; //Creates a new <div> element and adds the class info to it
  33.  
  34. var ul=document.createElement('div'); //Creates <ul> element
  35. info.appendChild(ul); //Adds the <ul> to the newly created <div>
  36.  
  37. var name=document.createElement('li');
  38. name.className='name'; //Should use class, not id, as ID must be unique
  39. name.innerHTML=employees[i].firstName+' '+employees[i].lastName; //Adds name
  40. ul.appendChild(name);
  41.  
  42. var time=document.createElement('li');
  43. time.className='time';
  44. time.innerHTML=employees[i].time;
  45. ul.appendChild(time);
  46.  
  47. var email=document.createElement('li');
  48. email.className='email';
  49. email.innerHTML=employees[i].email;
  50. ul.appendChild(email);
  51.  
  52. container.appendChild(info); //Adds the final generated HTML to the page
  53.  
  54. } //Will repeat for each item in list.
  55.  
  56. </script>
  57. </body>
  58. </html>
  59.  
  60. var employees=JSON.parse(txt).employees;
  61.  
  62. var container=//Link to the containing element using getElementById or similar
  63.  
  64. for (i=0;i<employees.length;i++) { //Loops for the length of the list
  65. var info=document.createElement('div');
  66. info.className='info'; //Creates a new <div> element and adds the class info to it
  67.  
  68. var ul=document.createElement('div'); //Creates <ul> element
  69. info.appendChild(ul); //Adds the <ul> to the newly created <div>
  70.  
  71. var name1=document.createElement('li'); //Chrome seems not to like the variable "name" in this instance
  72. name1.className='name'; //Should use class, not id, as ID must be unique
  73. name1.innerHTML=employees[i].firstName+' '+employees[i].lastName; //Adds name
  74. ul.appendChild(name1);
  75.  
  76. var time=document.createElement('li');
  77. time.className='time';
  78. time.innerHTML=employees[i].time;
  79. ul.appendChild(time);
  80.  
  81. var email=document.createElement('li');
  82. email.className='email';
  83. email.innerHTML=employees[i].email;
  84. ul.appendChild(email);
  85.  
  86. container.appendChild(info); //Displays the elements on the page
  87.  
  88. } //Will repeat for each item in list.
  89.  
  90. var employees = JSON.parse(txt).employees
  91.  
  92. var element = document.createElement('div'); // creates an empty div
  93.  
  94. someElement.append(otherElement); // makes otherElement a child of someElement
  95.  
  96. var json = eval('(' + txt + ')');
  97. alert(json.employees[0].firstName);
  98.  
  99. function insertItem(myid,position,newListItem) {
  100. var ul = document.getElementById(myid);
  101. var li = document.createElement("li");
  102. li.innerHTML=newListItem;
  103. ul.insertBefore(li, ul.getElementsByTagName("li")[position]);
  104. }
  105.  
  106. var txt = '{"employees":[' +
  107. '{"firstName":"John","lastName":"Doe","time":"9:15am","email":"john_doe@gmail.com" },' +
  108. '{"firstName":"Anna","lastName":"Smith","time":"9:15am","email":"anna@gmail.com" },' +
  109. '{"firstName":"Peter","lastName":"Jones" ,"time":"9:15am","email":"peter@gmail.com"}]}';
  110. txt = JSON.parse(txt); // normally after this you can check if txt is not null or something
  111.  
  112. var html = '<div class="info"><ul>';
  113. for (var a = 0; a < txt.employees.length; a++) {
  114. html += '<li id="name">'+txt.employees[a].firstName+' '+txt.employees[a].lastName+'</li>';
  115. html += '<li id="time">'+txt.employees[a].time+'</li>';
  116. html += '<li id="email">'+txt.employees[a].email+'</li>';
  117. }
  118. html += '</ul></div>';
  119.  
  120. document.getElementById('myoutput').innerHTML = html;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement