473,508 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

eval to create an object (JSON)

Hi,

I am gambling my soul and playing with eval(). I think this is similar
to JSON.

This script

var s = "{asdf:\"value\"}";
var o = eval(s);
var str = [];
for(p in o){
str.push(p + ":" + o[p]);
}
document.write("{" + str.join(", ") + "}");

outputs

{0:v, 1:a, 2:l, 3:u, 4:e}

but I expected

{asdf:value}

What's am I doing wrong (besides using eval())?

Thanks,
Peter

Apr 14 '06 #1
3 4527


pe**********@gmail.com wrote:

var s = "{asdf:\"value\"}"; outputs

{0:v, 1:a, 2:l, 3:u, 4:e}

but I expected

{asdf:value}

What's am I doing wrong (besides using eval())?


You need to use
var o = eval('(' + s + ')');
to ensure the curly braces are interpreted as the delimiters of an
object initializer (and not as block statement delimiters with one
labelled statement).
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 14 '06 #2

Martin Honnen wrote:
pe**********@gmail.com wrote:
var s = "{asdf:\"value\"}";

outputs

{0:v, 1:a, 2:l, 3:u, 4:e}

but I expected

{asdf:value}

What's am I doing wrong (besides using eval())?


You need to use
var o = eval('(' + s + ')');
to ensure the curly braces are interpreted as the delimiters of an
object initializer (and not as block statement delimiters with one
labelled statement).


Thanks Martin, it is working very well now:)

Peter

Apr 14 '06 #3

"Martin Honnen" <ma*******@yahoo.de> kirjoitti viestissä
news:44***********************@newsread2.arcor-online.net...

You need to use
var o = eval('(' + s + ')');
to ensure the curly braces are interpreted as the delimiters of an
object initializer (and not as block statement delimiters with one
labelled statement).


Thanks for the friendly and thorough answer. I had long wondered the reason
for parenthesis.
It has been a pleasure to read your posts, because of the human, polite and
emphatetic attitude.

After reading some robotlike or aggresive harsh comments every now and then
from some techically knowledgeable people your professional style saves my
day. Thanks.

---

If I were a doctor in a mental hospital, loosing my temper every now and
then would hardly make the hospital better and get more appreciation for my
knowlege. A friendly attitude would.
Apr 14 '06 #4

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

Similar topics

9
5489
by: Jim Washington | last post by:
I'm still working on yet another parser for JSON (http://json.org). It's called minjson, and it's tolerant on input, strict on output, and pretty fast. The only problem is, it uses eval(). It's...
24
3391
by: Larry | last post by:
Hi there: I have seen numerous postings about eval() and its evils on this forum. However, one of our developers is using it in the following way, which seems like a great use of it. Page...
2
60190
by: radykl | last post by:
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("(" +...
12
3758
by: Logos | last post by:
Yes, eval is a tool of the devil and I'll burn for using it. However, in this instance it's quite handy and I'm quite lazy. So, here's a weird one, and I'm wondering if anyone has a workaround....
2
3360
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..........
13
3153
by: My Pet Programmer | last post by:
The way I usually set up and work with the XMLHttpRequest to execute server side functions and get results is this: var url = "someurl?params=" + params; var conn = createRequest(); // gets an...
1
2667
by: bizt | last post by:
Hi, Im currently looking to move into using JSON for AJAX instead of returning from the server a string like the following: 12345{This is a text string{true I prefer objects because I dont...
0
255
by: Chris Rebert | last post by:
On Wed, Oct 8, 2008 at 5:34 PM, Warren DeLano <warren@delsci.comwrote: Assuming the data structures are sufficiently basic, i.e. no class instanciations, you can just use the json (AKA...
6
3540
Frinavale
by: Frinavale | last post by:
Apparently I have a lot of questions today regarding JavaScript security. I've implemented a JavaScript Object that intercepts page submits (postbacks) and then displays a UI prompting the user...
0
7224
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
7323
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
7379
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
5625
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,...
1
5049
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...
0
4706
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...
0
3192
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...
0
1550
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 ...
1
763
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.