5

I am facing issue with the following Json reponse object in the javascript eval function;Getting java script error expected } because of special characters Tamás and Török

{
  [{
    "userFirstNm": "Naresh",
    "userAsscId": "70336",
    "userLastNm": "Yashwantrao",
    "userLanId": "a70336"
  }, {
    "userFirstNm": "Tamás",
    "userAsscId": "37732",
    "userLastNm": "Török",
    "userLanId": "a37732"
  }]
}

Is there is any solution to resolve this problem.

alt text alt text

4
  • Could you show the code that's giving you errors? I tried this in my Firebug console and it behaved nicely. Commented Jun 22, 2010 at 4:26
  • I can't imagine this would not work. Commented Jun 22, 2010 at 4:26
  • that shouldn't be a problem i think so... Are you using jquery to parse it? Commented Jun 22, 2010 at 4:27
  • That is not valid JSON. Is it possible that you didn't want the outer braces? Commented Jun 22, 2010 at 4:41

3 Answers 3

4

Ah I know what the problem is. You need to wrap the object expression in parentheses for eval to work correctly.

alert(eval("({\"userFirstNm\":\"Tamás\",\"userAsscId\":\"37732\",\"userLastNm\":\"Török\",\"userLanId\":\"a37732\"})"));
Sign up to request clarification or add additional context in comments.

Comments

0

That isn't a JavaScript statement by itself, so you won't be able to eval it.

This Perl program runs the JavaScript in SpiderMonkey:

use warnings;
use strict;
use JavaScript::SpiderMonkey;
my $stuff = '{"userFirstNm":"Tamás","userAsscId":"37732","userLastNm":"Török","userLanId":"a37732"}';

my $stuff2 = "var k = new Object ($stuff)";

my $js2 = JavaScript::SpiderMonkey->new();
$js2->init();  # Initialize Runtime/Context
my $rc2 = $js2->eval($stuff2);
print "$@\n";

This doesn't print any error messages.

The following:

my $js = JavaScript::SpiderMonkey->new();
$js->init();  # Initialize Runtime/Context
my $rc = $js->eval($stuff);
print "$@\n";

produces

Error: SyntaxError: invalid label at line 1: {"userFirstNm":"Tam��s","userAsscId":"37732","userLastNm":"T��r��k","userLanId":"a37732"}

Comments

0

Put the string into a variable, and then put it into a var

var str = '{"userFirstNm":"Tamás","userAsscId":"37732","userLastNm":"Török","userLanId":"a37732"}';
eval("var obj=" + str);
console.debug ? console.debug(obj) : alert(obj); //outputs the object

And a safer alternative is the json_parse function: http://www.json.org/json_parse.js;

var obj = json_parse('{"userFirstNm":"Tamás","userAsscId":"37732","userLastNm":"Török","userLanId":"a37732"}');
console.debug ? console.debug(obj) : alert(obj); //outputs the object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.