json_encode/json_decode

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Please take a look at the following code and tell me what I'm doing wrong
here.  I'm just not understanding why this isn't working:

<?php

  $myArr = array( 'Key 1' => array( 'Key 1 1' => array( array( 'Key 1 1 1'
=> 'Value' ),
                                                        array( 'Key 1 1 2'
=> 'Value' )),
                                    'Key 1 2' => array( array( 'Key 1 2 1'
=> 'Value' ),
                                                        array( 'Key 1 2 2'
=> 'Value' ))),
                  'Key 2' => array( 'Key 2 1' => array( array( 'Key 2 1 1'
=> 'Value' ),
                                                        array( 'Key 2 1 2'
=> 'Value' )),
                                    'Key 2 2' => array( array( 'Key 2 2 1'
=> 'Value' ),
                                                        array( 'Key 2 2 2'
=> 'Value' ))));
  echo '$myArr: <pre>' . print_r( $myArr, TRUE ) . '</pre><br>';
  $myArrEncoded = json_encode( $myArr );
  echo var_dump( $myArrEncoded ) . '<br><br>';
  echo '$myArr encoded: ' . $myArrEncoded . '<br><br>';
  try {
    echo '$myArr decoded: <pre>' . json_decode( $myArrEncoded ) .
'</pre><br><br>';  // this is line 16
  } catch( Exception $e ) {
    echo 'Error: ' . var_dump( $e );
  }

?>


When I run the above, I get the following.  Please note that line 16 is in a
TRY block yet it still gives the error seen below:

$myArr:

Array
(
    [Key 1] => Array
        (
            [Key 1 1] => Array
                (
                    [0] => Array
                        (
                            [Key 1 1 1] => Value
                        )

                    [1] => Array
                        (
                            [Key 1 1 2] => Value
                        )

                )

            [Key 1 2] => Array
                (
                    [0] => Array
                        (
                            [Key 1 2 1] => Value
                        )

                    [1] => Array
                        (
                            [Key 1 2 2] => Value
                        )

                )

        )

    [Key 2] => Array
        (
            [Key 2 1] => Array
                (
                    [0] => Array
                        (
                            [Key 2 1 1] => Value
                        )

                    [1] => Array
                        (
                            [Key 2 1 2] => Value
                        )

                )

            [Key 2 2] => Array
                (
                    [0] => Array
                        (
                            [Key 2 2 1] => Value
                        )

                    [1] => Array
                        (
                            [Key 2 2 2] => Value
                        )

                )

        )

)


string(245) "{"Key 1":{"Key 1 1":[{"Key 1 1 1":"Value"},{"Key 1 1
2":"Value"}],"Key 1 2":[{"Key 1 2 1":"Value"},{"Key 1 2 2":"Value"}]},"Key
2":{"Key 2 1":[{"Key 2 1 1":"Value"},{"Key 2 1 2":"Value"}],"Key 2 2":[{"Key
2 2 1":"Value"},{"Key 2 2 2":"Value"}]}}"

$myArr encoded: {"Key 1":{"Key 1 1":[{"Key 1 1 1":"Value"},{"Key 1 1
2":"Value"}],"Key 1 2":[{"Key 1 2 1":"Value"},{"Key 1 2 2":"Value"}]},"Key
2":{"Key 2 1":[{"Key 2 1 1":"Value"},{"Key 2 1 2":"Value"}],"Key 2 2":[{"Key
2 2 1":"Value"},{"Key 2 2 2":"Value"}]}}


*Catchable fatal error*: Object of class stdClass could not be converted to
string in */path/to/my/file.php* on line *16


*Now, if I change it so that I pass the second parameter as TRUE for
json_decode (ie, change my code as follows:

    echo '$myArr decoded: <pre>' . json_decode( $myArrEncoded, TRUE ) .
'</pre><br><br>';  // this is line 16

), then here is what I get:

$myArr:

Array
(
    [Key 1] => Array
        (
            [Key 1 1] => Array
                (
                    [0] => Array
                        (
                            [Key 1 1 1] => Value
                        )

                    [1] => Array
                        (
                            [Key 1 1 2] => Value
                        )

                )

            [Key 1 2] => Array
                (
                    [0] => Array
                        (
                            [Key 1 2 1] => Value
                        )

                    [1] => Array
                        (
                            [Key 1 2 2] => Value
                        )

                )

        )

    [Key 2] => Array
        (
            [Key 2 1] => Array
                (
                    [0] => Array
                        (
                            [Key 2 1 1] => Value
                        )

                    [1] => Array
                        (
                            [Key 2 1 2] => Value
                        )

                )

            [Key 2 2] => Array
                (
                    [0] => Array
                        (
                            [Key 2 2 1] => Value
                        )

                    [1] => Array
                        (
                            [Key 2 2 2] => Value
                        )

                )

        )

)


string(245) "{"Key 1":{"Key 1 1":[{"Key 1 1 1":"Value"},{"Key 1 1
2":"Value"}],"Key 1 2":[{"Key 1 2 1":"Value"},{"Key 1 2 2":"Value"}]},"Key
2":{"Key 2 1":[{"Key 2 1 1":"Value"},{"Key 2 1 2":"Value"}],"Key 2 2":[{"Key
2 2 1":"Value"},{"Key 2 2 2":"Value"}]}}"

$myArr encoded: {"Key 1":{"Key 1 1":[{"Key 1 1 1":"Value"},{"Key 1 1
2":"Value"}],"Key 1 2":[{"Key 1 2 1":"Value"},{"Key 1 2 2":"Value"}]},"Key
2":{"Key 2 1":[{"Key 2 1 1":"Value"},{"Key 2 1 2":"Value"}],"Key 2 2":[{"Key
2 2 1":"Value"},{"Key 2 2 2":"Value"}]}}

$myArr decoded:

Array


What's going on here?  Why isn't json_decode() properly decoding my string?
Is there something more I need to do?

Any help would be greatly appreciated!

thnx,
Christoph

**

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux