Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 19th, 2013  |  syntax: None  |  size: 0.61 KB  |  views: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. testing.html:
  2. <!DOCTYPE html>
  3. <html>
  4. <body>
  5. <h2>Create Object from JSON String</h2>
  6. <p>
  7. First Name: <span id="fname"></span><br>
  8. Last Name: <span id="lname"></span><br>
  9. </p>
  10. <script src = smth.json></script>
  11. <script>
  12.  
  13. var obj = JSON.parse(txt)
  14.  
  15. document.getElementById("fname").innerHTML=obj.employees[1].firstName
  16. document.getElementById("lname").innerHTML=obj.employees[1].lastName
  17. </script>
  18. </body>
  19. </html>
  20.  
  21. smth.json:
  22.  
  23. txt = {
  24.     "employees": [
  25.         {"firstName":"John",
  26.         "lastName":"Doe" },
  27.         {"firstName":"Anna",
  28.         "lastName":"Smith" },
  29.         {"firstName":"Peter",
  30.         "lastName":"Jones" }
  31. ]};
clone this paste RAW Paste Data