Pass Json Object to a Function As Parameter

Discussion in 'Javascript' started by vunet, Dec 17, 2008.

  1. vunet

    vunet Guest

    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.
    vunet, Dec 17, 2008
    #1
    1. Advertising

  2. vunet wrote:
    > 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"} )


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Dec 17, 2008
    #2
    1. Advertising

  3. vunet

    vunet Guest

    On Dec 17, 1:20 pm, Martin Honnen <> wrote:
    > vunet wrote:
    > > 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"} )
    >
    > --
    >
    >         Martin Honnen
    >        http://JavaScript.FAQTs.com/


    Thanks, I already got that. Appreciate...
    vunet, Dec 17, 2008
    #3
  4. vunet

    David Mark Guest

    On Dec 17, 1:03 pm, vunet <> wrote:
    > 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.
    David Mark, Dec 17, 2008
    #4
  5. vunet

    RobG Guest

    On Dec 18, 4:22 am, vunet <> wrote:
    > On Dec 17, 1:20 pm, Martin Honnen <> wrote:
    >
    >
    >
    > > vunet wrote:
    > > > 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:


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


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


    Perhaps:

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


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


    --
    Rob
    RobG, Dec 18, 2008
    #5
  6. Martin Honnen wrote:

    > vunet wrote:
    > > 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"} )


    js> function f( o )
    { Print( o.prop1, "\n" )
    }
    js> f( {prop1:"value1", prop2:"value2"} )
    value1
    William James, Dec 18, 2008
    #6
  7. David Mark a écrit :
    > On Dec 17, 1:03 pm, vunet <> wrote:
    >> 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.
    >


    David, do you really think bashing people will contribute to raise their
    (sometime very poor, indeed) javascript knowledge ?
    Bruno Desthuilliers, Dec 18, 2008
    #7
  8. vunet

    Jorge Guest

    On Dec 18, 3:54 am, Conrad Lender <> wrote:
    > On 2008-12-18 01:56, RobG wrote:
    > ...
    > > I think the OP meant to pass a JSON string, not an object.  If that's
    > > correct, eval is OK.

    >
    > 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.

    --
    Jorge.
    Jorge, Dec 18, 2008
    #8
  9. vunet

    Bart Lateur Guest

    vunet wrote:

    >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"}' ));

    --
    Bart.
    Bart Lateur, Dec 18, 2008
    #9
  10. Bruno Desthuilliers wrote:

    > David Mark a écrit :
    >> On Dec 17, 1:03 pm, vunet <> wrote:
    >>> This doe snot work for me. o is undefined. Please suggest if possible.

    >>
    >> 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 ?


    Second that.
    Daniel Smedegaard Buus, Dec 19, 2008
    #10
  11. vunet

    David Mark Guest

    On Dec 19, 4:02 am, Daniel Smedegaard Buus
    <> wrote:
    > Bruno Desthuilliers wrote:
    > > David Mark a écrit :
    > >> On Dec 17, 1:03 pm, vunet <> wrote:
    > >>> This doe snot work for me. o is undefined. Please suggest if possible..

    >
    > >> 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 ?

    >
    > 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.
    David Mark, Dec 19, 2008
    #11
  12. vunet

    David Mark Guest

    On Dec 18, 4:25 am, Bruno Desthuilliers <bruno.
    > wrote:
    > David Mark a écrit :
    >
    >
    >
    > > On Dec 17, 1:03 pm, vunet <> wrote:
    > >> 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.

    >
    > 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.
    David Mark, Dec 19, 2008
    #12
  13. David Mark a écrit :
    > On Dec 19, 4:02 am, Daniel Smedegaard Buus
    > <> wrote:
    >> Bruno Desthuilliers wrote:
    >>> David Mark a écrit :
    >>>> On Dec 17, 1:03 pm, vunet <> wrote:
    >>>>> This doe snot work for me. o is undefined. Please suggest if possible.
    >>>> 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 ?

    >> Second that.

    >
    > 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).
    Bruno Desthuilliers, Dec 19, 2008
    #13
  14. vunet

    David Mark Guest

    On Dec 19, 7:05 am, Bruno Desthuilliers <bruno.
    > wrote:
    > David Mark a crit :
    >
    > > On Dec 19, 4:02 am, Daniel Smedegaard Buus
    > > <> wrote:
    > >> Bruno Desthuilliers wrote:
    > >>> David Mark a crit :
    > >>>> On Dec 17, 1:03 pm, vunet <> wrote:
    > >>>>> This doe snot work for me. o is undefined. Please suggest if possible.
    > >>>> 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 ?
    > >> Second that.

    >
    > > This is not a help desk

    >
    > So you're not required to answer !-)


    But I gave him the answer anyway. What is your issue with that?

    [snip]
    David Mark, Dec 19, 2008
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    216
    Florian Frank
    Jun 30, 2009
  2. AzamSharp
    Replies:
    2
    Views:
    149
  3. sajuptpm
    Replies:
    2
    Views:
    299
    sajuptpm
    Dec 28, 2012
  4. Acácio Centeno
    Replies:
    1
    Views:
    229
    dieter
    Feb 15, 2013
  5. Bryan Britten
    Replies:
    9
    Views:
    248
    Bryan Britten
    May 28, 2013
Loading...

Share This Page