Pass Json Object to a Function As Parameter

V

vunet

How do I pass JSON obj to a function:

function do( o ){
var obj = eval("(" + o + ")");
alert(obj.prop1)
}

do( {"prop1":"value1","prop2":"value2"} )

This doe snot work for me. o is undefined. Please suggest if possible.
 
M

Martin Honnen

vunet said:
How do I pass JSON obj to a function:

function do( o ){
var obj = eval("(" + o + ")");
alert(obj.prop1)
}

do( {"prop1":"value1","prop2":"value2"} )

This doe snot work for me. o is undefined. Please suggest if possible.

You can't name a function 'do', that is a keyword.

And you don't need eval at all:

function f( o ){
alert(o.prop1);
}

f( {"prop1":"value1","prop2":"value2"} )
 
V

vunet

You can't name a function 'do', that is a keyword.

And you don't need eval at all:

function f( o ){
    alert(o.prop1);

}

f( {"prop1":"value1","prop2":"value2"} )

Thanks, I already got that. Appreciate...
 
D

David Mark

How do I pass JSON obj to a function:

function do( o ){

Don't call functions "do" or "while" or "for", etc.
   var obj = eval("(" + o + ")");

What sort of magical spell is this?
   alert(obj.prop1)
window.alert(o.prop1);


}

do( {"prop1":"value1","prop2":"value2"} )

This doe snot work for me. o is undefined. Please suggest if possible.

I suggest you take up a new hobby.
 
R

RobG

I think the OP meant to pass a JSON string, not an object. If that's
correct, eval is OK.


Perhaps:

function f(o) {
if (typeof o == 'string') {
o = eval('(' + o + ')');
}
return o && o.prop1;
}


f('{"prop1":"value1","prop2":"value2"}');
 
W

William James

Martin said:
You can't name a function 'do', that is a keyword.

And you don't need eval at all:

function f( o ){
alert(o.prop1);
}

f( {"prop1":"value1","prop2":"value2"} )

js> function f( o )
{ Print( o.prop1, "\n" )
}
js> f( {prop1:"value1", prop2:"value2"} )
value1
 
B

Bruno Desthuilliers

David Mark a écrit :
Don't call functions "do" or "while" or "for", etc.


What sort of magical spell is this?


I suggest you take up a new hobby.

David, do you really think bashing people will contribute to raise their
(sometime very poor, indeed) javascript knowledge ?
 
J

Jorge

On 2008-12-18 01:56, RobG wrote:
...

I think there's some general confusion between JSON and object literals.
Not sure if that's the case for the OP, but I often see people writing
things like "you can pass a JSON object to this function". There's no
such thing as a JSON object; JSON is a string format.

A picky question.

ISTM that the text [ 1.2e5 ] is valid JSON text and the string
'[ 1.2e5 ]' *contains* valid JSON text... JSON specifies the format of
a text, not the nature of the text's container.
 
B

Bart Lateur

vunet said:
How do I pass JSON obj to a function:

function do( o ){
var obj = eval("(" + o + ")");
alert(obj.prop1)
}

do( {"prop1":"value1","prop2":"value2"} )

This doe snot work for me. o is undefined. Please suggest if possible.

There's a mismatch between the kind of parameter you pass and the kind
of parameter you're thinking of. To make this work, and really behave
like JSON, the parameter for the call must be a (JSON) string, not an
object.

function doh( o ){
var obj = eval("(" + o + ")");
alert(obj.prop1)
}

// pass a JSON *string*, not an object
doh( '{"prop1":"value1","prop2":"value2"}' );


But probably it's best to separate the deserialisation from the action:

function deserialize( o ){ // convert JSON string to object
return eval("(" + o + ")");
}

function doh( obj ){
alert(obj.prop1)
}

doh( deserialize('{"prop1":"value1","prop2":"value2"}' ));
 
D

Daniel Smedegaard Buus

Bruno said:
David Mark a écrit :

David, do you really think bashing people will contribute to raise their
(sometime very poor, indeed) javascript knowledge ?

Second that.
 
D

David Mark

Second that.

This is not a help desk (or a city council meeting.) Posting
gobbledygook is a waste of everybody's time. Encouraging such
behavior doesn't help the poster either as they will just do it again.
 
D

David Mark

David Mark a écrit :







David, do you really think bashing people will contribute to raise their
(sometime very poor, indeed) javascript knowledge ?

Do you think giving them the answer, *as well as* a needed dose of
reality is helpful? Regardless, mind your own posts.
 
B

Bruno Desthuilliers

David Mark a écrit :
This is not a help desk

So you're not required to answer !-)
(or a city council meeting.)

Nope, but everyone may profit from some feedback sometimes...
Posting
gobbledygook is a waste of everybody's time. Encouraging such
behavior doesn't help the poster either as they will just do it again.

Correcting a clueless poster doesn't requires being uselessly rude. The
usual result of useless rudeness is that clueless poster ends up not
asking for help anymore, so there's no chance he'll get corrected. But
be sure he'll still try to write code.

Granted, there are some people that will never get programming right -
and when the fact becomes obvious (that is, when there's no sign the
poster learns anything from previous Q/A), advising another hobby might
be the better answer - but at this point, while it may be perceived as
rude, it's at least not _uselessly_ rude.

Just my 2 cents, really. (and thread's over for me - I've exposed my
point and won't come back on this anymore).
 

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

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top