473,796 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using eval to parse a JSON text

Can anyone explain me why you need to add open and close parenthesis to
a JSON text in order to use eval() to parse it?
For example:

var json = "{a: 'abc', b: 'def'}";
var obj1 = eval("(" + json + ")"); //ok!
var obj2 = eval(json); //syntax error!

why are parenthesis necessary?

Thanks!

/Andres.

Jun 9 '06 #1
2 60215

ra****@gmail.co m wrote:
Can anyone explain me why you need to add open and close parenthesis to
a JSON text in order to use eval() to parse it?
For example:

var json = "{a: 'abc', b: 'def'}";
var obj1 = eval("(" + json + ")"); //ok!
var obj2 = eval(json); //syntax error!

why are parenthesis necessary?


For the same reson as:

{a: 'abc', b: 'def'};

causes an error but:

( {a: 'abc', b: 'def'} );

does not.

When encountered on the left hand side, the punctuator '{' defines the
start of a block statement, like if{... or while{... etc. The stuff
inside the block is evaluated as if it were a series of statements, so
the script engine attempts to evaluate:

a: 'abc', b: 'def'

and barfs (understandably ).

By enclosing the expression in () it is evaluated as if it were the
right hand side of an expression, in which case {} is treated as an
object initialiser. So what you must pass to eval is:

( { /* property names & values */ } )

as a literal string to force it to treat the {} as an object
initialiser.
--
Rob

Jun 9 '06 #2
ra****@gmail.co m wrote:
var json = "{a: 'abc', b: 'def'}";
var obj1 = eval("(" + json + ")"); //ok!


Rob already stated the reason. But I'm wondering why you wouldn't use
eval("var obj1="+json);
instead?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jun 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
8624
by: Bri | last post by:
Greetings, I am using Eval() in a query with only limited success. If the text within the function contains a reference to a Field I get #ERROR#. I'll try and explain what I'm trying to do and maybe one of you can either tell me how to get the Eval() to work or suggest and alternate method. I have a table called Units: Fields: Unit, Formula Sample Data:
1
7089
by: Patrick De Ridder | last post by:
When I code Double.Parse(textBox1.Text) I get an error How can I convert a text box entry to a numeric? Please give a code example, if you know the answer.
1
1993
by: Joel Byrd | last post by:
I've been using an eval() statement, which has been working fine until I put it inside of a function. Is this a known problem, and is there a known solution/work-around to this?
2
2081
by: webteam | last post by:
I've ran into a little trouble with Eval, hoping someone can point it out to me This code works : var_ktml = "textarea2" Set ktml_textarea2 = new ktml4 Eval("ktml_" & var_ktml).Init var_ktml This code doesn't :
8
2177
by: abhishek | last post by:
>>a,b=3,4 7 Now I want to evaluate y by substituting for the evaluated value of x. eval(y) will try to add "a+b" to 3 and return an error. I could do this, 10 but this becomes unwieldy if I have and so on, because the replacements have to be done in exactly the
13
9296
by: Genki | last post by:
Can you figure out how to do this in Access2002/VBA? I want to get the value of an object property. The trick is that the name of the property to retrieve is stored in a table. Here's how I've set it up: I have a class module "objPerson" which has a property "FirstName". This works: objPerson.FirstName = "John Smith" strFirstName = objPerson.FirstName
7
5065
by: Darko | last post by:
Hello, I have this particular problem with eval() when using Microsoft Internet Explorer, when trying to define an event handler. This is the code: function BigObject() { this.items = new Array(); this.values = new Array();
2
3402
by: bylabylamo | last post by:
i have a problem converting a json string that i get from a servlet into a json object using eval function of java script. any one please help me..........
5
1968
by: Smiley | last post by:
I'm fooling around with using Eval and trying to manipulate a few things. I ran into a couple of weird results. First of all, in one place I used the following code: $filestring = str_replace("<?", "\n<?\n", $filestring); $filestring = str_replace("?>", "\n?>\n", $filestring); Not a huge thing, just making things easier to read for me. But doing this gives me an error, even when I comment those lines out. I have to remove them...
0
9530
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10017
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...
0
9055
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6793
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
5445
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
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.