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 ...

+ Reply to Thread
Results 1 to 4 of 4

json_encode/json_decode, take 2

  1. Default 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:&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


  2. Default 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:


    &quot; 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.

  3. Default 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:

    >
    > &quot; 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


  4. Default 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:&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
    >


+ Reply to Thread