Using eval('(' + evalText + ')')

C

Chuck Anderson

This seems like a very basic question - but I have searched Google and
the javascript faq and I cannot find an explanation.

What is the purpose of using eval in this manner (and this would be my
usage - data returned from an XMLHttpRequest).

var data = eval('(' + req.responseText + ')');

Why the inner parenthesis?

TIA

--
*****************************
Chuck Anderson • Boulder, CO
http://www.cycletourist.com
Turn Off, Tune Out, Drop In
*****************************
 
K

Kiran Makam

var data = eval('(' + req.responseText + ')');

In JavaScript, "{" and "}" are used to denote
- a block of code and
- an object literal

when you execute eval( "{'key':value}" ), "{" is executed as a block
of code instead of an object literal. Hence inner parenthesis is used
to force eval to treat the json-string as an object literal.

You can go through this for detailed explanation:
http://rayfd.wordpress.com/2007/03/28/why-wont-eval-eval-my-json-or-json-object-object-literal/

Kiran Makam
 
J

Jorge

(...)
You can go through this for detailed explanation:http://rayfd.wordpress.com/2007/03/28/why-wont-eval-eval-my-json-or-j...

<quote>
Note something about this parser though: it is REALLY strict. It won’t
parse perfectly OK object literals like { name: “Ray”, age: 31 }. Eval
parses this without problems:
</quote>

It's a common error to build JSON texts without the required enclosing
quotes in the object's properties' names, but such not-really-JSON
texts won't pass the eval() when/if any of the properties' names are
=== to a JS reserved word :

eval('( { do: 5, while: 10 } )'); -> err
eval('( { "do": 5, "while": 10 } )'); -> ok

or when/if the properties' names just somehow look awful to the JS
parser:

eval('( { : 5, *: 6, how many: 7 } )'); -> err
eval('( { "": 5, "*":6, "how many": 7 } )'); -> ok


IMO, this things ought to be in the FAQ:

http://www.google.com/search?&q=JSON+site:jibbering.com/faq/

"Your search - JSON site:jibbering.com/faq/ - did not match any
documents."

: pitiful.
 
C

Chuck Anderson

Kiran said:
In JavaScript, "{" and "}" are used to denote
- a block of code and
- an object literal

when you execute eval( "{'key':value}" ), "{" is executed as a block
of code instead of an object literal. Hence inner parenthesis is used
to force eval to treat the json-string as an object literal.

You can go through this for detailed explanation:
http://rayfd.wordpress.com/2007/03/28/why-wont-eval-eval-my-json-or-json-object-object-literal/

Kiran Makam

Ahhhhh ... syntactic ambiguity. Why didn't you just say so? ô¿Ô¬

No wonder I couldn't figure it out for myself.

So .... while I'm on this topic. I'm sending data back to an
XMLHttpRequest from Php and not even using JSON. I merely use:

<?php
echo "var somevar1 = 'somevalue1';";
echo "var somevar2 = 'somevalue2';";
exit;
?>

Then I just "eval(req.responseText);"

Is there any need for me to convert this to an object and use JSON
instead when simply passing back a group of simple variables?

<?php
$json = array()
$json['somevar1'] = 'somevalue1';
$json['somevar2'] = 'somevalue2';
$json_encoded = json_encode($json);
echo $json;
exit;
?>

--
*****************************
Chuck Anderson • Boulder, CO
http://www.cycletourist.com
Turn Off, Tune Out, Drop In
*****************************
 
P

Peter Michaux

Ahhhhh ... syntactic ambiguity. Why didn't you just say so? ô¿Ô¬

No wonder I couldn't figure it out for myself.

So .... while I'm on this topic. I'm sending data back to an
XMLHttpRequest from Php and not even using JSON. I merely use:

<?php
echo "var somevar1 = 'somevalue1';";
echo "var somevar2 = 'somevalue2';";
exit;
?>

Then I just "eval(req.responseText);"

Is there any need for me to convert this to an object and use JSON
instead when simply passing back a group of simple variables?

Yes there is a good reason to use JSON rather than arbitrary
JavaScript: non-browser clients. JSON is a data interchange format and
libraries to read and write JSON exist in many languages. If a non-
browser client is consuming your data in JSON format then the client
only needs a JSON library. If your server sends back arbitrary
JavaScript then either ad-hoc parsing or a full JavaScript parser
would be necessary and either of these is an ugly solution.
Transporting data in a data format, like JSON, for flexibility is a
good idea.

Peter
 
G

Garrett Smith

Kiran said:
In JavaScript, "{" and "}" are used to denote
- a block of code and
- an object literal

when you execute eval( "{'key':value}" ), "{" is executed as a block
of code instead of an object literal. Hence inner parenthesis is used
to force eval to treat the json-string as an object literal.

That ought to be in the FAQ under:
http://jibbering.com/faq/#eval

with a link to http://json.org/

Garrett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,098
Messages
2,570,625
Members
47,236
Latest member
EverestNero

Latest Threads

Top