eval and JSON problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
dcinadr
Forum Newbie
Posts: 12
Joined: Fri Jan 08, 2010 4:15 pm

eval and JSON problem

Post by dcinadr »

I encoding a php array with json_encode and then trying to use eval in javascript to decode the array. It is not working. Nothing happens - not even an error. If I debug my javascript and step over the line that runs eval() it never goes to the next line of code.

if I print out the literal string output from coming from the php output it looks like this:
"{"oldRec":"325","newRec":"424","messages":["This is line 325","This is line 326","This is line 327","This is line 328","This is line 329","This is line 330","This is line 331","This is line 332","This is line 333","This is line 334","This is line 335","This is line 336","This is line 337","This is line 338","This is line 339","This is line 340","This is line 341","This is line 342","This is line 343","This is line 344","This is line 345","This is line 346","This is line 347","This is line 348 (error)","This is line 349 (error)","This is line 350 (error)","This is line 351 (error)","This is line 352","This is line 353 (error)","This is line 354","This is line 355","This is line 356","This is line 357","This is line 358","This is line 359","This is line 360","This is line 361","This is line 362","This is line 363","This is line 364","This is line 365","This is line 366","This is line 367","This is line 368","This is line 369","This is line 370","This is line 371","This is line 372","This is line 373","This is line 374","This is line 375","This is line 376","This is line 377","This is line 378","This is line 379","This is line 380","This is line 381","This is line 382","This is line 383","This is line 384 (error)","This is line 385","This is line 386 (error)","This is line 387","This is line 388","This is line 389","This is line 390","This is line 391","This is line 392","This is line 393","This is line 394","This is line 395","This is line 396","This is line 397","This is line 398","This is line 399","This is line 400","This is line 401","This is line 402","This is line 403","This is line 404","This is line 405","This is line 406","This is line 407","This is line 408","This is line 409","This is line 410","This is line 411","This is line 412","This is line 413","This is line 414","This is line 415","This is line 416","This is line 417","This is line 418","This is line 419","This is line 420","This is line 421","This is line 422","This is line 423","This is line 424"]}"

what is the problem? why won't the eval function work?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: eval and JSON problem

Post by Eran »

Can you show us the code that performs the eval?
dcinadr
Forum Newbie
Posts: 12
Joined: Fri Jan 08, 2010 4:15 pm

Re: eval and JSON problem

Post by dcinadr »

Code: Select all

 
xmlHttp2 = GetXmlHttpObject();
    if (xmlHttp2 == null)
        return;
    xmlHttp2.onreadystatechange = function() {
            if (xmlHttp2.readyState == 4) {
                var jsonText = xmlHttp2.responseText;
                [color=#FF4000]var jsonObject = eval(jsonText);[/color]
                displayLogMessages(jsonObject);
                //document.getElementById("messages_p").innerHTML = xmlHttp2.responseText;
            }
        };
    avoidCache = parseInt (Math.random()*99999999);
    
    sPage = "logMgr.php?" + avoidCache + "&type=" + type + "&start=" + start;
    
    
    xmlHttp2.open ("GET",sPage, true);
    xmlHttp2.send (null);
 
it does not get past the code in red
dcinadr
Forum Newbie
Posts: 12
Joined: Fri Jan 08, 2010 4:15 pm

Re: eval and JSON problem

Post by dcinadr »

here is the php code as well

Code: Select all

 
$logCollection = array("oldRec" => $oldRec, "newRec" => $newRec, "messages" => $messages);
echo json_encode($logCollection);
 
$oldRec is string
$newrec is string
$messages is array
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: eval and JSON problem

Post by Eran »

try changing the eval command to this:

Code: Select all

var jsonObject = eval('(' + jsonText + ')');
Also, I'm assuming you've checked that jsonText contains the data you are expecting?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: eval and JSON problem

Post by AbraCadaver »

I thought you needed the actual assignement in the evaled code?

Code: Select all

var jsonText = xmlHttp2.responseText;
jsonText = "someVar = " + jsonText;
var jsonObject = eval(jsonText);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dcinadr
Forum Newbie
Posts: 12
Joined: Fri Jan 08, 2010 4:15 pm

Re: eval and JSON problem

Post by dcinadr »

pytrin wrote:try changing the eval command to this:

Code: Select all

var jsonObject = eval('(' + jsonText + ')');
Also, I'm assuming you've checked that jsonText contains the data you are expecting?
pytrin - You're awesome!!! That was it. Thanks!!!!
Post Reply