Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > JSON: Array of object literals?

Reply
Thread Tools

JSON: Array of object literals?

 
 
Keith Hughitt
Guest
Posts: n/a
 
      07-01-2009
Hi all,

I've been testing out the native JSON support in Firefox 3.5, and have
run into some issues. I was wondering if someone might be able to help
me figure out what is going on.

I would like to be able to encode and decode an array of JavaScript
object literals, however, the parser doesn't seem to like having an
array as the outermost container, e.g.:

// Okay
JSON.parse(JSON.stringify({name: "bob",id: 3}));

// Okay
JSON.parse(JSON.stringify({objects: ["bob", 3]}));

// Not Okay
JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name: "john",id:
4}]));

// Doesn't work, either
JSON.parse(JSON.stringify([1,2,3]));

All of the above should be valid JSON. So why doesn't the above work?

Any feedback would be greatly appreciated.

Thanks!
Keith
 
Reply With Quote
 
 
 
 
Keith
Guest
Posts: n/a
 
      07-01-2009
Update: Works in Prototype & jQuery (with JSON plugin).

For example:

//Okay
Object.toJSON([{name: "bob",id: 3}, {name: "john",id:4}]).evalJSON
();

// Also okay
$.evalJSON($.toJSON([{name: "bob",id: 3}, {name: "john",id:4}]));

Still need to test json2.js. For now though it looks like this problem
is restricted primarily to Firefox's native JSON support :\




 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-01-2009
Keith Hughitt wrote:
> [native JSON support in Firefox 3.5]
> the parser doesn't seem to like having an array as the outermost
> container, e.g.:
>
> // Okay
> JSON.parse(JSON.stringify({name: "bob",id: 3}));
>
> // Okay
> JSON.parse(JSON.stringify({objects: ["bob", 3]}));
>
> // Not Okay
> JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name: "john",id:
> 4}]));
>
> // Doesn't work, either
> JSON.parse(JSON.stringify([1,2,3]));
>
> All of the above should be valid JSON. So why doesn't the above work?


<http://jibbering.com/faq/#posting>
<http://jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork>
<http://catb.org/~esr/faqs/smart-questions.html>
<http://catb.org/~esr/faqs/smart-questions.html#examples>


PointedEars
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-01-2009
Keith wrote:
> Update: Works in Prototype & jQuery (with JSON plugin).


So what? There are other, more important things that don't even remotely
work there.


PointedEars
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      07-01-2009
Keith Hughitt wrote:
> (...)
>
> // Not Okay
> JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name: "john",id:
> 4}]));
>
> // Doesn't work, either
> JSON.parse(JSON.stringify([1,2,3]));
>
> All of the above should be valid JSON. So why doesn't the above work?
>
> Any feedback would be greatly appreciated.


>>> JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name:

"john",id:4}]))[1]['name'];
--> "john"

>>> JSON.parse(JSON.stringify([1,2,3]));

--> [1, 2, 3]

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.1pre)
Gecko/20090701 Shiretoko/3.5.1pre

--
Jorge.
 
Reply With Quote
 
Eric Bednarz
Guest
Posts: n/a
 
      07-01-2009
Keith Hughitt <> writes:

> I've been testing out the native JSON support in Firefox 3.5,


Yesterday’s official release or a preview version, what platform, etc?

> I would like to be able to encode and decode an array of JavaScript
> object literals, however, the parser doesn't seem to like having an
> array as the outermost container,


Define “doesn't seem to like”, as in what outcome do you expect and what
outcome do you observe in what context.

> e.g.:
>
> // Okay
> JSON.parse(JSON.stringify({name: "bob",id: 3}));
>
> // Okay
> JSON.parse(JSON.stringify({objects: ["bob", 3]}));
>
> // Not Okay
> JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name: "john",id:
> 4}]));
>
> // Doesn't work, either
> JSON.parse(JSON.stringify([1,2,3]));
>
> All of the above should be valid JSON.


All of the above works for me. Bummer

> So why doesn't the above work?


So far you are the only one with enough information to find the answer
to that question. Define the environment you are using and provide a
complete test case.
 
Reply With Quote
 
wilq
Guest
Posts: n/a
 
      07-02-2009
On Jul 1, 9:53*pm, Keith Hughitt <keith.hugh...@gmail.com> wrote:
> Hi all,
>
> I've been testing out the native JSON support in Firefox 3.5, and have
> run into some issues. I was wondering if someone might be able to help
> me figure out what is going on.
>
> I would like to be able to encode and decode an array of JavaScript
> object literals, however, the parser doesn't seem to like having an
> array as the outermost container, e.g.:
>
> * * // Okay
> * * JSON.parse(JSON.stringify({name: "bob",id: 3}));
>
> * * // Okay
> * * JSON.parse(JSON.stringify({objects: ["bob", 3]}));
>
> * * // Not Okay
> * * JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name: "john",id:
> 4}]));
>
> * * // Doesn't work, either
> * * JSON.parse(JSON.stringify([1,2,3]));
>
> All of the above should be valid JSON. So why doesn't the above work?
>
> Any feedback would be greatly appreciated.
>
> Thanks!
> Keith


I'm not sure if thats the case, but from what I remember JSON is more
strict about names of object attribute. Try instead of doing name:
"bob" to do "name": "bob" . Does that help ?
 
Reply With Quote
 
Keith
Guest
Posts: n/a
 
      07-02-2009
Hi all,

Thanks for taking the time to test the code snippets I posted, and
replying. Sorry I didn't provide more information in the original post
regarding the environment I ran the code in. Here it is now:

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.1pre) Gecko/
20090731 Shiretoko/3.5.1pre
Firebug 1.4.0b3
Prototype 1.6.0.3
jQuery 1.3.2

The code snippets were run in Firebug, and also in small test pages
where the output was simply dumped into a container on the screen.

In all of the above, the goal was to be able to successfully encode
(stringify) and then decode (parse) an array of object literals of the
form:

[
{
name: "bob",
id: 3
},
{
name: "john",
id: 4
}
]

So the expected output is a JavaScript object (exactly the same as the
input), and the output I was getting instead was a string of the form:

"[{"name": "bob", "id": 3}, {"name": "john", "id": 4}]"

Finally. I did test both with and without quotes key names, which
although helping the JSON to qualify as "valid" (e.g. http://www.jsonlint.com/),
made no difference
in the above tests.

I have been able to track down the issue, and it turns out that it is
related to a conflict with Prototype (tested: 1.6.0.3 and 1.6.1rc3).
Here is a simple file that can verify the problem:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">
<html>
<head>
<title>Native JSON Test</title>

<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/
prototype.js" type="text/javascript"></script>

<script type="text/javascript">
var test = function () {
//console.log(JSON.parse(JSON.stringify([{"name": "bob", "id":
3}, {"name": "john", "id": 4}])));
var output = document.getElementById("output");
output.innerHTML = JSON.parse(JSON.stringify([{"name": "bob",
"id": 3}, {"name": "john", "id": 4}]));
};
</script>

</head>
<body onload="test();">
<div id="output"></div>
</body>
</html>

Run first as is and you should see:

[{"name": "bob", "id": 3}, {"name": "john", "id": 4}]

which means that the stringified JSON was not parsed correctly, and is
returning a string instead of an object.

Next, try commenting out the Prototype include and running again. You
should now see:

[object Object],[object Object]

Success! (you can verify that it is indeed the same object by running
the function in Firebug).

If someone else could verify this for me on a different setting, it
would be much appreciated.

Thanks all,
Keith
 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      07-02-2009
Keith meinte:

> Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.1pre) Gecko/
> 20090731 Shiretoko/3.5.1pre
> Firebug 1.4.0b3
> Prototype 1.6.0.3
> jQuery 1.3.2


[snip]

> I have been able to track down the issue, and it turns out that it is
> related to a conflict with Prototype (tested: 1.6.0.3 and 1.6.1rc3).


Surprise, surprise...

Gregor


--
http://www.gregorkofler.com
http://web.gregorkofler.com - vxJS, a JS lib in progress
 
Reply With Quote
 
Eric Bednarz
Guest
Posts: n/a
 
      07-02-2009
Keith <> writes:

> Sorry I didn't provide more information in the original post
> regarding the environment I ran the code in. Here it is now:
>
> Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.1pre) Gecko/
> 20090731 Shiretoko/3.5.1pre
> Firebug 1.4.0b3
> Prototype 1.6.0.3
> jQuery 1.3.2


Oh well. Let’s hope you learned something from that.

(Frankly, I totally fail to understand how anyone could include huge
libraries in test cases for testing native browser features, especially
new ones, where incompatibilities haven’t been blogged about to death,
or however that works nowadays

> Finally. I did test both with and without quotes key names, which
> although helping the JSON to qualify as "valid" (e.g. http://www.jsonlint.com/),
> made no difference
> in the above tests.


Richard already explained why that doesn’t matter.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM
Problem assigning an Array object to an Array-subclass object Richard Lionheart Ruby 27 05-04-2004 06:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57