CosmoCode
  • Great software.

  • Bright people.

  • Happy customers!

CosmoCode GmbH
  • Home
  • Skills
  • About Us
  • References
  • Blog
  • Open Source
←
All blogposts
→

JSON and for-each pitfalls

JSON is a simple exchange-format for datastructures. It is widely used when 'ajaxing' the web, because it bypasses the XML layer of the underlying XMLHTTPRequest. But -  be prepared to run in problems when using JSON. JSON adds methods to all Objects and Arrays (using the prorotype's of Array, String and Object). So you have to be prepared to see functions like “toJSONString” when iterating over members.

Detlef Hüttemann, 06/27/2006 10:48 a.m.

JSON and for-each pitfalls

JSON is a simple exchange-format for datastructures. It is widely used when 'ajaxing' the web, because it bypasses the XML layer of the underlying XMLHTTPRequest.

But -  be prepared to run in problems when using JSON. JSON adds methods to all Objects and Arrays (using the prorotype's of Array, String and Object). So you have to be prepared to see functions like “toJSONString” when iterating over members.

Especially the for-each loop on arrays might crash. The following loop iterates over the array's elements.

<script src=”/js/json.js” /> <script>

  var names = [ 'foo','bar' ];

  for each ( f in names ) {     alert( f );   }

</script>

Running this script in a browser will show you foo,bar and toJSONString - this is probably not what you are expecting!

To avoid errors in your application, you have to check the members type - but this will break the elegance of the for-each loop .

Read more

  • JSON and Rhino Pitfalls
  • rhino again
  • Ausprobiert: rome
  • Weihnachtskalender
  • neues ajax-Modul
  • ajax rulez!
  • JavaScript: Fixing the Closure Scope in Loops
  • Bildschirm dekorieren mit Yahoo!
  • JavaScript closures and the mysterious this
  • JQuery

Contact

Thank you for your interest!
Get in contact:

CosmoCode GmbH

Prenzlauer Allee 36G
10405 Berlin

Phone: +49 30 814 50 40 70

Fax: +49 30 2809 7093


mail: info@cosmocode.de

CosmoCode GmbH  
   

© CosmoCode 2021 | Imprint | Data Privacy | Cookies verwalten

Close
Deutsch English
  • Jobs