473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

simple json help

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
9 1673
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post...
9
by: Jonathan Fine | last post by:
Hello I want to serialise a dictionary, whose keys and values are ordinary strings (i.e. a sequence of bytes). I can of course use pickle, but it has two big faults for me. 1. It should not...
1
by: seth | last post by:
Hi: I'm trying to read JSON strings sent from the browser. Here is the scenario: 1. Using YUI tookit 2. sending JSON string from YUI toolkit - using the provided asyncRequest method. **I...
19
RMWChaos
by: RMWChaos | last post by:
Previously, I had used independent JSON lists in my code, where the lists were part of separate scripts. Because this method did not support reuse of a script without modification, I decided to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.