By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,509 Members | 1,288 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,509 IT Pros & Developers. It's quick & easy.

simple json help

P: n/a
eval is not working on this JSON string:

{"top":["Br","We","Tr","MTL","Fu","Mos","Fo"]}
{"car":["Ve","Cin","Spr","Al","Ne","AT"]}
{"man":["No","Mo","LG","Ky","Sam","Pan","Au"]}
data = eval('(' + json + ')');

What's wrong with the json string, is it 'cause it has no root?

Mar 8 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Mar 8, 12:45 pm, "egg...@gmail.com" <egg...@gmail.comwrote:
eval is not working on this JSON string:

{"top":["Br","We","Tr","MTL","Fu","Mos","Fo"]}
{"car":["Ve","Cin","Spr","Al","Ne","AT"]}
{"man":["No","Mo","LG","Ky","Sam","Pan","Au"]}

data = eval('(' + json + ')');

What's wrong with the json string, is it 'cause it has no root?
It has syntax errors. An object consists of name:value pairs, you
have effectively just assigned a bunch of values (c.f. an array).

The name:value pairs in an object literal must be a comma separated
list.

The quotes around the property names are not required, you might find
it easier to read without them.

Try:

{
top: ["Br","We","Tr","MTL","Fu","Mos","Fo"],
car: ["Ve","Cin","Spr","Al","Ne","AT"],
man: ["No","Mo","LG","Ky","Sam","Pan","Au"]
}

Note there is no final comma ','.

--
Rob

Mar 8 '07 #2

P: n/a
eg****@gmail.com wrote:
eval is not working on this JSON string:

{"top":["Br","We","Tr","MTL","Fu","Mos","Fo"]}
{"car":["Ve","Cin","Spr","Al","Ne","AT"]}
{"man":["No","Mo","LG","Ky","Sam","Pan","Au"]}
data = eval('(' + json + ')');

What's wrong with the json string, is it 'cause it has no root?
A JSON text has to represent an array or an object. You have three objects. You
can either evaluate then individually, or you can put them in an array.

http://www.JSON.org/
Mar 8 '07 #3

P: n/a
On Mar 7, 8:18 pm, "RobG" <r...@iinet.net.auwrote:
On Mar 8, 12:45 pm, "egg...@gmail.com" <egg...@gmail.comwrote:
eval is not working on this JSON string:
{"top":["Br","We","Tr","MTL","Fu","Mos","Fo"]}
{"car":["Ve","Cin","Spr","Al","Ne","AT"]}
{"man":["No","Mo","LG","Ky","Sam","Pan","Au"]}
data = eval('(' + json + ')');
What's wrong with the json string, is it 'cause it has no root?

It has syntax errors. An object consists of name:value pairs, you
have effectively just assigned a bunch of values (c.f. an array).

The name:value pairs in an object literal must be a comma separated
list.

The quotes around the property names are not required, you might find
it easier to read without them.

Try:

{
top: ["Br","We","Tr","MTL","Fu","Mos","Fo"],
car: ["Ve","Cin","Spr","Al","Ne","AT"],
man: ["No","Mo","LG","Ky","Sam","Pan","Au"]
}

Note there is no final comma ','.

--
Rob
OK, that works! Question: what's the significance of not putting
quotes around "top, car and man" like in your example? Regarding the
final comma, I've ran into that issue before, it crashes in IE right?
I've spent like a day on that once....

Mar 8 '07 #4

P: n/a
On Mar 8, 5:47 am, Douglas Crockford <nos...@sbcglobal.netwrote:
egg...@gmail.com wrote:
eval is not working on this JSON string:
{"top":["Br","We","Tr","MTL","Fu","Mos","Fo"]}
{"car":["Ve","Cin","Spr","Al","Ne","AT"]}
{"man":["No","Mo","LG","Ky","Sam","Pan","Au"]}
data = eval('(' + json + ')');
What's wrong with the json string, is it 'cause it has no root?

A JSON text has to represent an array or an object. You have three objects. You
can either evaluate then individually, or you can put them in an array.

http://www.JSON.org/
Hey, you're the JSON guy! Thanks for JSON.

Mar 8 '07 #5

P: n/a
On Mar 8, 5:47 am, Douglas Crockford <nos...@sbcglobal.netwrote:
egg...@gmail.com wrote:
eval is not working on this JSON string:
{"top":["Br","We","Tr","MTL","Fu","Mos","Fo"]}
{"car":["Ve","Cin","Spr","Al","Ne","AT"]}
{"man":["No","Mo","LG","Ky","Sam","Pan","Au"]}
data = eval('(' + json + ')');
What's wrong with the json string, is it 'cause it has no root?

A JSON text has to represent an array or an object. You have three objects. You
can either evaluate then individually, or you can put them in an array.

http://www.JSON.org/
Also, did you know the JSON.js library you provide with your site has
incompatibilities with the prototype.js library?

Mar 8 '07 #6

P: n/a
RobG <rg***@iinet.net.auwrote:
Note there is no final comma ','.
it seems that's browser dependant, Firefox ignores the final comma,
Safari find an error with it.
--
Une Bévue
Mar 8 '07 #7

P: n/a
On Mar 9, 2:47 am, unbewusst.s...@google.com.invalid (Une Bévue)
wrote:
RobG <r...@iinet.net.auwrote:
Note there is no final comma ','.

it seems that's browser dependant, Firefox ignores the final comma,
Safari find an error with it.
Regardless, it is a syntax error in an Object literal and not
tolerated by IE or Opera either.

Trailing commas are not a syntax error in an Array literal, however
they are incorrectly handled in some browsers and therefore not
advised.
--
Rob

Mar 8 '07 #8

P: n/a
eg****@gmail.com wrote:
On Mar 8, 5:47 am, Douglas Crockford <nos...@sbcglobal.netwrote:
>egg...@gmail.com wrote:
>>eval is not working on this JSON string:
{"top":["Br","We","Tr","MTL","Fu","Mos","Fo"]}
{"car":["Ve","Cin","Spr","Al","Ne","AT"]}
{"man":["No","Mo","LG","Ky","Sam","Pan","Au"]}
data = eval('(' + json + ')');
What's wrong with the json string, is it 'cause it has no root?
A JSON text has to represent an array or an object. You have three objects. You
can either evaluate then individually, or you can put them in an array.

http://www.JSON.org/

Also, did you know the JSON.js library you provide with your site has
incompatibilities with the prototype.js library?
Send a bug report to Prototype.
Mar 9 '07 #9

P: n/a
RobG <rg***@iinet.net.auwrote:
>
Regardless, it is a syntax error in an Object literal and not
tolerated by IE or Opera either.

Trailing commas are not a syntax error in an Array literal, however
they are incorrectly handled in some browsers and therefore not
advised.
fine thanks for this point i didn't notice the difference between object
and array for that point.
--
Une Bévue
Mar 9 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.