Advertisement
Guest User

Untitled

a guest
Feb 19th, 2013
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. main:
  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 = '{"employees":[ "{firstName":"John","lastName":"Doe" }', '{"firstName":"Anna","lastName":"Smith" }', '{"firstName":"Peter","lastName":"Jones" }
  24. ]}';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement