> > > From: Pops
> > > I figured it out know.  You got to look at the 
> > > constructor type to see if its an Object, Array
> > > or String.   From there you can decide to use
> > > for each or for in or for loop.

> > From: Michael Geary
> > You don't have to write that code yourself:
> > http://jollytoad.googlepages.com/json.js

> Yeah sure. but 90% of the battle is always understanding 
> other people's code.  For me to master a language (in this 
> case JS/jQuery), I have to roll my own, trial and error, live 
> and learn, understanding the basis and then I would be able 
> to say "ahhhh, ok, I can understand how this.js or that.js 
> relates and how I can use it or not use it.
> Too much overhead". Etc

You're a lot like me that way. And it's not that much work to build a simple
JSON serializer. But as you can see from json.js, there are just enough
weird little cases to make it "interesting". :-)

> IE doesn't like:
> 
>     json = {};
> 
> but will accept:
> 
>    var json = {};

Let me take a guess...

You are executing this code inside a function, and you have an HTML element
in your page with the id 'json'.

Unlike other browsers, IE creates a read-only property of the window object
for every id in your page. If you have an element whose ID is 'json', then
window.json is a reference to that object.

"var json = {}" succeeds because it creates a local variable inside the
function named 'json', so there is no conflict. "json = {}" fails because it
attempts to create a window.json property, but that property already exists
and is read-only.

If you were to execute "var json = {}" in the global scope (outside any
function), it would fail too.

Did I get it right?

> Also, I did try the original json.js from json.org and there 
> I learned that I needed to understand another concept - 
> pulling just the array from a jQuery result. This is because 
> the JSON parser was creating json for all the properties.  I 
> then realized the jQuery .get() method purpose in life.

That's true, .get() does return a full-blooded Array object (unlike the
jQuery result object which does have numbered elements and a .length
property but is not a true Array).

But I don't see how that helps you with JSON. The jQuery result object is an
array of DOM elements, and there is no JSON representation for those. So you
can't directly convert a jQuery result object into any kind of JSON format.

-Mike

Reply via email to