On Sun, May 2, 2010 at 05:59, julian-w <[email protected]> wrote:

>
> Hello,
> I want to convert an Array to a JSON-String, but it doesn't work.
> Here's an example:  http://tinyurl.com/2vood6s http://tinyurl.com/2vood6s
>
> I only get "[]" :(
>

As Jim pointed out, you created an Array but then "indexed" it with strings.
There is no such thing in JavaScript as an associative array like this, as
is done in PHP or some other languages. What's happening here is that you're
attaching properties to the Array object. Many operations will appear to
work, but you get into the realm of browser dependencies. You might, for
example, think that you can use the following syntax to enumerate the keys:

  for (var key in JSON)
  {
    alert("JSON[" + key + "]=" + JSON[key]);
  }

With some browsers that'll work fine. With others, you'll get any methods
which have been added to the native Array (done in qooxdoo to emulate
certain methods that exist in other browser implementations... and hopefully
being removed soon) as well as the key values that you've added.

The general rule is: JavaScript does not have associative arrays. Instead,
use JavaScript Objects which provide the exact behavior one would expect in
this circumstance.

Hope that adds a bit of illumination to the story.

Cheers,

Derrell
------------------------------------------------------------------------------
_______________________________________________
qooxdoo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to