IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Traitement json bonne pratiques


Sujet :

Langage PHP

  1. #1
    Membre actif
    Inscrit en
    Septembre 2004
    Messages
    450
    Détails du profil
    Informations forums :
    Inscription : Septembre 2004
    Messages : 450
    Points : 267
    Points
    267
    Par défaut Traitement json bonne pratiques
    Bonjour,

    J'ai réussit a faire ce que je voulais faire, a savoir extraire les champs "allow_embed", "country", "views_total", mais je me demandé si il n'y avait pas plus simple ?
    Et aussi un meilheur contrôle des données ?

    voici le code source :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <?php
    $myTweets = '{"page":1,"limit":2,"has_more":true,"list":[{"allow_embed":true,"country":"FR","views_total":7069},{"allow_embed":true,"country":"FR","views_total":4996}]}';
     
    $myJSONTweets = json_decode($myTweets,TRUE);
    var_dump($myJSONTweets);
    foreach ($myJSONTweets as $t){
    		if(is_array($t)){
    				var_dump($t);
    				for($i=0;$i<count($t);$i++){
    						echo "allow_embed: ".$t[$i]['allow_embed']."<br>";
    						echo "country: ".$t[$i]['country']."<br>";
    						echo "views_total: ".$t[$i]['views_total']."<br>";
    				}
    		}
    }
    ?>
    Voici le résultat:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    array
      'page' => int 1
      'limit' => int 2
      'has_more' => boolean true
      'list' => 
        array
          0 => 
            array
              'allow_embed' => boolean true
              'country' => string 'FR' (length=2)
              'views_total' => int 7069
          1 => 
            array
              'allow_embed' => boolean true
              'country' => string 'FR' (length=2)
              'views_total' => int 4996
     
    array
      0 => 
        array
          'allow_embed' => boolean true
          'country' => string 'FR' (length=2)
          'views_total' => int 7069
      1 => 
        array
          'allow_embed' => boolean true
          'country' => string 'FR' (length=2)
          'views_total' => int 4996
     
    allow_embed: 1
    country: FR
    views_total: 7069
    allow_embed: 1
    country: FR
    views_total: 4996
    merci beaucoup

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    347
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 347
    Points : 203
    Points
    203
    Par défaut
    Bonjour,

    moi je ferrai comme ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <?php
     
    $myTweets = '{"page":1,"limit":2,"has_more":true,"list":[{"allow_embed":true,"country":"FR","views_total":7069},{"allow_embed":true,"country":"FR","views_total":4996}]}';
     
    $myJSONTweets = json_decode($myTweets, true);
     
    if (is_array($myJSONTweets['list']))
    {
    	foreach($myJSONTweets['list'] as $myTweet)
    	{
    		echo "allow_embed: ".$myTweet['allow_embed']."<br>";
    		echo "country: ".$myTweet['country']."<br>";
    		echo "views_total: ".$myTweet['views_total']."<br>";
    	}
    }

  3. #3
    Membre actif
    Inscrit en
    Septembre 2004
    Messages
    450
    Détails du profil
    Informations forums :
    Inscription : Septembre 2004
    Messages : 450
    Points : 267
    Points
    267
    Par défaut
    Excellent !
    merci beaucoup

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Bonnes pratiques de protections individuelles
    Par Community Management dans le forum Sécurité
    Réponses: 23
    Dernier message: 11/06/2024, 11h23
  2. [2008] Traitement SSAS bonne pratique
    Par fishing1 dans le forum SSAS
    Réponses: 2
    Dernier message: 07/04/2014, 17h33
  3. [Bonne pratique]Stratégie d'allocation
    Par jowo dans le forum C
    Réponses: 1
    Dernier message: 05/10/2005, 14h47
  4. [FOREIGN K] Valeur de champ = nom de table. Bonne pratique ?
    Par Seb des Monts dans le forum Langage SQL
    Réponses: 9
    Dernier message: 17/05/2005, 10h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo