json_encode/json_decode, take 2 - PHP
This is a discussion on json_encode/json_decode, take 2 - PHP ; 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 ...
-
json_encode/json_decode, take 2
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:"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"}}})";
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
-
Re: [PHP] json_encode/json_decode, take 2
Christoph Boget wrote:
> 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:
" is not what you think it is (but that could be my mail client),
and the exterior parentheses are also incorrect. your try/catch block is doing
nothing because neither function throws exceptions.
-
Re: [PHP] json_encode/json_decode, take 2
On 10/19/07, Jochem Maas <jochem@iamjochem.com> wrote:
>
> Christoph Boget wrote:
> > 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:
>
> " is not what you think it is (but that could be my mail client),
> and the exterior parentheses are also incorrect. your try/catch block is
> doing
> nothing because neither function throws exceptions.
i thought it might be something like that, but i didnt spend long enough at
it to verify.
here is a great tool to help:
http://www.jslint.com/
-nathan
-
Re: [PHP] json_encode/json_decode, take 2
try seeing what json_encode() creates:
$json = "json";
$data = array(
"no_1" => $json,
"no_2" => $json,
"no_3" => $json,
"no_4" => array("foo" => $json, "bar" = $json),
);
print_r({"{$json}_encode"}($data));
Christoph Boget wrote:
> 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:"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"}}})";
>
> 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
>