PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > json_encode/json_decode
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
json_encode/json_decode

Réponse
 
LinkBack Outils de la discussion
Vieux 19/10/2007, 17h05   #1
Christoph Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut json_encode/json_decode

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 would be greatly appreciated!

thnx,
Christoph

**

  Réponse avec citation
Vieux 19/10/2007, 17h07   #2
Christoph Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: json_encode/json_decode

Please disregard. Sometimes I weep at my own stupidity... :p

thnx,
Christoph

On 10/19/07, Christoph Boget <christoph.boget@gmail.com> wrote:
>
> 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:


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09840 seconds with 10 queries