subramani
subramani

Reputation: 1069

Json eval function in javascript

var jsonCallbackCode1 = eval("employees = { 'accounting' : [ { 'firstName' : 'Jo''hn', 'lastName'  : 'Doe','age': 23 }]}");
alert(employees.accounting[0].firstName);

I got the following exception

Microsoft JScript compilation error: Expected '}'

help me?

Upvotes: 1

Views: 1653

Answers (4)

Tom Bartel
Tom Bartel

Reputation: 2258

I believe

'Jo''hn'

is the problem.

Upvotes: 0

AmbrosiaDevelopments
AmbrosiaDevelopments

Reputation: 2592

Try this... you have to use two backslashes to escape fully:

var jsonCallbackCode1 = eval("employees = { 'accounting' : [ { 'firstName' : 'Jo\\'\\'hn', 'lastName'  : 'Doe','age': 23 }]}");
            alert(employees.accounting[0].firstName);

Or of course you could just remove the apostrophes from the firstName altogether.

Upvotes: 1

ant
ant

Reputation: 22948

Try this

var jsonCallbackCode1 = eval("employees = { 'accounting' : [ { 'firstName' : 'Jo\'\'hn', 'lastName'  : 'Doe','age': 23 }]}");
    alert(employees.accounting[0].firstName);

Upvotes: 0

axel_c
axel_c

Reputation: 6796

The parser is choking on The 'Jo''hn' because of the single quote. Escape it with \'

Upvotes: 0

Related Questions