473,805 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1691
On Mar 8, 12:45 pm, "egg...@gmail.c om" <egg...@gmail.c omwrote:
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.co m 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.c om" <egg...@gmail.c omwrote:
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...@sbcglob al.netwrote:
egg...@gmail.co m 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...@sbcglob al.netwrote:
egg...@gmail.co m 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
incompatibiliti es with the prototype.js library?

Mar 8 '07 #6
RobG <rg***@iinet.ne t.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.inva lid (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.co m wrote:
On Mar 8, 5:47 am, Douglas Crockford <nos...@sbcglob al.netwrote:
>egg...@gmail.c om 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
incompatibiliti es with the prototype.js library?
Send a bug report to Prototype.
Mar 9 '07 #9
RobG <rg***@iinet.ne t.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
2705
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 he calls what I am suggesting "brittle", his own Javascript JSON parser is not valid JSON, but rather conforms to my proposed variation on JSON!! With an identifier prepended to the front of the JSON block, and function literals as values: see...
9
3643
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 be used with untrusted data. 2. I want non-Python programs to be able to read and write these dictionaries.
1
2303
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 would like to read the JSON string sent from the client on the
19
1992
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 consolidate all my JSON lists into one and modify my scripts so that they were more generic and reusable. So far so good. The problem is that my JSON lists used variables for many pieces of code that performed multiple iterations to create several...
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10360
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.