What is wrong with my JSON string?

Discussion in 'Javascript' started by asleepatdesk, Feb 6, 2007.

  1. asleepatdesk

    asleepatdesk Guest

    Hi,

    I need some help here. When I try to eval() my AJAX returned JSON
    string, I continually get a javascript error "Expected )".

    Here's my JSON string:
    {"recs": [{"RepKey": "RepKey", "RepID": "RepID", "Type": "Type"},
    {"RepKey": "1", "RepID": "000", "Type": "RTRR"},
    {"RepKey": "2", "RepID": "0M2", "Type": "RTRR"},
    {"RepKey": "3", "RepID": "1DV", "Type": "RTRR"} ]};

    My js function simply tries to eval() it:
    var jsonStr = eval('(' + str + ')');

    When I write str to a div, I get the above JSON string without js
    errors. This is driving me crazy. Any help would be hugely
    appreciated.

    Thanks,
    asleep
    asleepatdesk, Feb 6, 2007
    #1
    1. Advertising

  2. On Feb 5, 5:23 pm, "asleepatdesk" <> wrote:
    > Hi,
    >
    > I need some help here. When I try to eval() my AJAX returned JSON
    > string, I continually get a javascript error "Expected )".
    >
    > Here's my JSON string:
    > {"recs": [{"RepKey": "RepKey", "RepID": "RepID", "Type": "Type"},
    > {"RepKey": "1", "RepID": "000", "Type": "RTRR"},
    > {"RepKey": "2", "RepID": "0M2", "Type": "RTRR"},
    > {"RepKey": "3", "RepID": "1DV", "Type": "RTRR"} ]};
    >
    > My js function simply tries to eval() it:
    > var jsonStr = eval('(' + str + ')');
    >
    > When I write str to a div, I get the above JSON string without js
    > errors. This is driving me crazy. Any help would be hugely
    > appreciated.
    >
    > Thanks,
    > asleep


    Get rid of the ; at the end.

    ({asdf:'foo'};) is wrong.
    ({asdf:'foo'}) is ok.

    --
    Isaac Z. Schlueter
    http://isaacschlueter.com
    Isaac Schlueter, Feb 6, 2007
    #2
    1. Advertising

  3. asleepatdesk

    asleepatdesk Guest

    On Feb 5, 5:53 pm, "Isaac Schlueter" <> wrote:
    > On Feb 5, 5:23 pm, "asleepatdesk" <> wrote:
    >
    >
    >
    >
    >
    > > Hi,

    >
    > > I need some help here. When I try to eval() my AJAX returned JSON
    > > string, I continually get a javascript error "Expected )".

    >
    > > Here's my JSON string:
    > > {"recs": [{"RepKey": "RepKey", "RepID": "RepID", "Type": "Type"},
    > > {"RepKey": "1", "RepID": "000", "Type": "RTRR"},
    > > {"RepKey": "2", "RepID": "0M2", "Type": "RTRR"},
    > > {"RepKey": "3", "RepID": "1DV", "Type": "RTRR"} ]};

    >
    > > My js function simply tries to eval() it:
    > > var jsonStr = eval('(' + str + ')');

    >
    > > When I write str to a div, I get the above JSON string without js
    > > errors. This is driving me crazy. Any help would be hugely
    > > appreciated.

    >
    > > Thanks,
    > > asleep

    >
    > Get rid of the ; at the end.
    >
    > ({asdf:'foo'};) is wrong.
    > ({asdf:'foo'}) is ok.
    >
    > --
    > Isaac Z. Schlueterhttp://isaacschlueter.com- Hide quoted text -
    >
    > - Show quoted text -


    Thanks a lot! That worked. I do have one more question. In the
    example that I'm using, the value from the JSON object is retrieved
    with a specific reference to the object member name: page['players']
    .lastName. Is it possible to retrieve the value of the object
    member without specifically knowing that it was called "lastName"?
    The way my app is being developed, the JSON object is created based on
    the the recordset returned from sql. Since the sql field names will
    always be different, resulting in different JSON object member names,
    I want to retrieve whatever is in page['players'].

    Thanks again.
    asleep
    asleepatdesk, Feb 6, 2007
    #3
  4. All hail the mighty IN.

    for( var i in page.players ) {
    for( var j in page.players) {
    alert('page.players['+i+'].'+j+' = '+page.players[j]);
    }
    }

    IN is also a boolean test.

    if( 'asdf' in page.players ) {
    alert('the '+i+' player has a asdf and it is '+page.players.asdf);
    }

    --
    Isaac Z. Schlueter
    http://isaacschlueter.com
    Isaac Schlueter, Feb 6, 2007
    #4
    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:
    227
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    315
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    241
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    255
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    153
    David Karr
    Jun 17, 2013
Loading...

Share This Page