By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,510 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,510 IT Pros & Developers. It's quick & easy.

What is wrong with my JSON string?

P: n/a
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

Feb 6 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Feb 5, 5:23 pm, "asleepatdesk" <s...@scoleman.netwrote:
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

Feb 6 '07 #2

P: n/a
On Feb 5, 5:53 pm, "Isaac Schlueter" <isaacschlue...@gmail.comwrote:
On Feb 5, 5:23 pm, "asleepatdesk" <s...@scoleman.netwrote:


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

Feb 6 '07 #3

P: n/a
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

Feb 6 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.