Posted By

qrist0ph on 03/16/09


Tagged

ajax


Versions (?)

JSON Parsing


 / Published in: HTML
 

  1. <head>
  2. <title>Titel der Webseite</title>
  3.  
  4. <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
  5. <!-- Parser from rom http://www.json.org/ -->
  6. <script type="text/javascript" src="json2.js"></script>
  7. <script type="text/javascript">
  8. /*
  9. * * Pyhton REQUEST Processing
  10. * ------
  11. * email = request.GET.get('name', False)
  12. * passwort = request.GET.get('password', False)
  13. *
  14. * Pyhton RESPONSE
  15. * -----
  16. * return HttpResponse(simplejson.dumps({'sessionid':request.session.id}),0, mimetype='application/javascript')
  17. *
  18. */
  19. function parseJson(){
  20. /*
  21. * WITH eval() WORKS FINE
  22. */
  23. var jsonString ='{ name: "Ein Name", liste: [1,"zwei",3] }';
  24. var myObject = eval('(' + jsonString + ')');
  25. alert("eval1 " + myObject.name + " " + myObject.liste[1]);
  26.  
  27. /*
  28. * WITH eval() WORKS FINE
  29. */
  30. var jsonString ='{ "name": "Ein Name", liste: [1,"zwei",3] }';
  31. var myObject = eval('(' + jsonString + ')');
  32. alert("eval2 " + myObject.name);
  33.  
  34. /*
  35. * WITH JSON Parser WORKS FINE
  36. */
  37. var jsonString ='{ "name": "Ein Name", "liste": [1,"zwei",3] }';
  38. var myObject = JSON.parse(jsonString);
  39. alert("JSON Parser " +myObject.name);
  40.  
  41. /*
  42. * WITH JSON Parser DOESNT WORK
  43. */
  44. var jsonString ='{ name: "Ein Name", liste: [1,"zwei",3] }';
  45. var myObject = JSON.parse(jsonString);
  46. alert("JSON Parser " +myObject.name);
  47. }
  48.  
  49. /*
  50. * Ajax XmlHttpRequest senden
  51. */
  52. function sendData(){
  53. $.getJSON('/login_ajax/', {
  54. name: $("#username").val(),
  55. password: "einPassword"
  56. }, login_callback);
  57. }
  58. </script>
  59. </head>
  60. <body ">
  61.  
  62. <input type="button" value="Json Test" onclick="parseJson()">
  63. </body>
  64. </html>

Report this snippet  

You need to login to post a comment.