json_encode/json_decode, take 2

From: Date: Fri, 19 Oct 2007 16:51:19 +0000
Subject: json_encode/json_decode, take 2
Groups: php.general 
Request: Send a blank email to php-general+get-263399@lists.php.net to get a copy of this message
The string used below in "$myArrEncoded" is generated in javascript, after creating the structure and spitting out: var JSONVar = javascriptVar.toSource(); I can eval JSONVar and work with it as I would be working with the original javascriptVar so I know the transition back and forth from a structure to a string isn't causing problems. The problem is when I try to work with the string in PHP. Here is my code: <?php $myArrEncoded = "({Salary:{'50-70K':{filterType:&quot;range&quot;, fieldName:&quot;SALARY&quot;, fieldValueLabel:&quot;50-70K&quot;, lowerValue:&quot;50&quot;, upperValue:&quot;70&quot;, inclusive:&quot;BOTH&quot;}}, Position:{Developer:{filterType:&quot;single&quot;, fieldName:&quot;POSITION&quot;, fieldValueLabel:&quot;Developer&quot;, fieldValue:&quot;Developer&quot;, constraint:&quot;equals&quot;}, SysAdmin:{filterType:&quot;single&quot;, fieldName:&quot;POSITION&quot;, fieldValueLabel:&quot;System Admin&quot;, fieldValue:&quot;SysAdmin&quot;, constraint:&quot;equals&quot;}}, 'Required Action':{'2 - GenScreen':{filterType:&quot;single&quot;, fieldName:&quot;REQUIRED_ACTION&quot;, fieldValueLabel:&quot;General Phone Screen&quot;, fieldValue:&quot;2 - GenScreen&quot;, constraint:&quot;equals&quot;}}})"; echo var_dump( $myArrEncoded ) . '<br><br>'; echo '$myArr encoded: ' . $myArrEncoded . '<br><br>'; try { echo '$myArr decoded: <pre>' . print_r( json_decode( $myArrEncoded, TRUE ), TRUE ) . '</pre><br><br>'; } catch( Exception $e ) { echo 'Error: ' . var_dump( $e ); } ?> When I run it, I get the following output: -------------------------------------------------------------------------------- string(587) "({Salary:{'50-70K':{filterType:"range", fieldName:"SALARY", fieldValueLabel:"50-70K", lowerValue:"50", upperValue:"70", inclusive:"BOTH"}}, Position:{Developer:{filterType:"single", fieldName:"POSITION", fieldValueLabel:"Developer", fieldValue:"Developer", constraint:"equals"}, SysAdmin:{filterType:"single", fieldName:"POSITION", fieldValueLabel:"System Admin", fieldValue:"SysAdmin", constraint:"equals"}}, 'Required Action':{'2 - GenScreen':{filterType:"single", fieldName:"REQUIRED_ACTION", fieldValueLabel:"General Phone Screen", fieldValue:"2 - GenScreen", constraint:"equals"}}})" $myArr encoded: ({Salary:{'50-70K':{filterType:"range", fieldName:"SALARY", fieldValueLabel:"50-70K", lowerValue:"50", upperValue:"70", inclusive:"BOTH"}}, Position:{Developer:{filterType:"single", fieldName:"POSITION", fieldValueLabel:"Developer", fieldValue:"Developer", constraint:"equals"}, SysAdmin:{filterType:"single", fieldName:"POSITION", fieldValueLabel:"System Admin", fieldValue:"SysAdmin", constraint:"equals"}}, 'Required Action':{'2 - GenScreen':{filterType:"single", fieldName:"REQUIRED_ACTION", fieldValueLabel:"General Phone Screen", fieldValue:"2 - GenScreen", constraint:"equals"}}}) $myArr decoded: ({Salary:{'50-70K':{filterType:"range", fieldName:"SALARY", fieldValueLabel:"50-70K", lowerValue:"50", upperValue:"70", inclusive:"BOTH"}}, Position:{Developer:{filterType:"single", fieldName:"POSITION", fieldValueLabel:"Developer", fieldValue:"Developer", constraint:"equals"}, SysAdmin:{filterType:"single", fieldName:"POSITION", fieldValueLabel:"System Admin", fieldValue:"SysAdmin", constraint:"equals"}}, 'Required Action':{'2 - GenScreen':{filterType:"single", fieldName:"REQUIRED_ACTION", fieldValueLabel:"General Phone Screen", fieldValue:"2 - GenScreen", constraint:"equals"}}}) -------------------------------------------------------------------------------- Why isn't json_decode() converting the string to an array? Is there something extra in there that json_decode() can't deal with? I can work with it fine in javascript... :( thnx, Christoph

« previous php.general (#263399) next »