HighDots Forums  

What is wrong with my JSON string?

Javascript JavaScript language (comp.lang.javascript)


Discuss What is wrong with my JSON string? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
asleepatdesk
 
Posts: n/a

Default What is wrong with my JSON string? - 02-05-2007 , 08:23 PM






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


Reply With Quote
  #2  
Old   
Isaac Schlueter
 
Posts: n/a

Default Re: What is wrong with my JSON string? - 02-05-2007 , 08:53 PM






On Feb 5, 5:23 pm, "asleepatdesk" <s... (AT) scoleman (DOT) net> wrote:
Quote:
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



Reply With Quote
  #3  
Old   
asleepatdesk
 
Posts: n/a

Default Re: What is wrong with my JSON string? - 02-06-2007 , 01:57 PM



On Feb 5, 5:53 pm, "Isaac Schlueter" <isaacschlue... (AT) gmail (DOT) com> wrote:
Quote:
On Feb 5, 5:23 pm, "asleepatdesk" <s... (AT) scoleman (DOT) net> 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']
[i].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'][i].

Thanks again.
asleep



Reply With Quote
  #4  
Old   
Isaac Schlueter
 
Posts: n/a

Default Re: What is wrong with my JSON string? - 02-06-2007 , 02:40 PM



All hail the mighty IN.

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

IN is also a boolean test.

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

--
Isaac Z. Schlueter
http://isaacschlueter.com


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.