Précédent   Forum des professionnels en informatique > PHP > Langage > Fonctions
Fonctions Forum d'entraide sur les fonctions PHP. Avant de poster -> FAQ fonctions et Sources diverses
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 19/05/2011, 18h52   #1
Invité régulier
 
Thomas
Inscription : mai 2010
Messages : 24
Détails du profil
Informations personnelles :
Nom : Thomas
Âge : 21

Informations forums :
Inscription : mai 2010
Messages : 24
Points : 8
Points : 8
Par défaut Parser du json

Bonjour,

Je souhaiterais parser le code json ci-dessous pour extraire dans cet exemple test :

Code :
1
2
3
4
5
6
7
8
9
{
 "data": {
  "translations": [
   {
    "translatedText": "test"
   }
  ]
 }
}
J'ai essayé ceci :
Code :
1
2
$obj = json_decode($json);
print $obj->{'data'->'translations'->'translatedText'};
Mais cela ne marche pas.

Merci d'avance,
Thomas.
nox73 est déconnecté   Envoyer un message privé Réponse avec citation 01
Vieux 19/05/2011, 19h20   #2
Modérateur
 
Inscription : septembre 2010
Messages : 7 103
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 103
Points : 8 466
Points : 8 466
juste ça normalement :
Code :
1
2
 
$obj->data->translations->translatedText;
fait un print_r($obj) et tu comprendras
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 01
Vieux 19/05/2011, 19h41   #3
Invité régulier
 
Thomas
Inscription : mai 2010
Messages : 24
Détails du profil
Informations personnelles :
Nom : Thomas
Âge : 21

Informations forums :
Inscription : mai 2010
Messages : 24
Points : 8
Points : 8
Merci de ta réponse mais sa ne fonctionne pas (Notice: Trying to get property of non-object in C:\wamp\www\***\json.php on line 10)
Lorsque je fais un print_r j'obtient :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
stdClass Object
(
    [data] => stdClass Object
        (
            [translations] => Array
                (
                    [0] => stdClass Object
                        (
                            [translatedText] => profiter
                        )
 
                )
 
        )
 
)
En faite c'est plus la POO qui me pose problème ..
nox73 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/05/2011, 19h43   #4
Modérateur
 
Inscription : septembre 2010
Messages : 7 103
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 103
Points : 8 466
Points : 8 466
donc :
Code :
$obj->data->translations[0]->translatedText;
si tu le veux en array c'est avec le 2eme paramètre de json_decode
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/05/2011, 19h52   #5
Invité régulier
 
Thomas
Inscription : mai 2010
Messages : 24
Détails du profil
Informations personnelles :
Nom : Thomas
Âge : 21

Informations forums :
Inscription : mai 2010
Messages : 24
Points : 8
Points : 8
Au TOP,
Merci beaucoup de ton aide !
Thomas.
nox73 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 19h50.


 
 
 
 
Partenaires

Hébergement Web