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

JavaScript Discussion :

Parser un JSON


Sujet :

JavaScript

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    60
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 60
    Points : 42
    Points
    42
    Par défaut Parser un JSON
    Bonjour,

    J'ai un petit problème de JSON ... j'ai un flux type article , et j'aimerais récupérer chacune de ses lignes( "text": ) dans un tableau .
    Je vous mets mon fichier en dessous ... n'hésitez pas si vous avez des idées :

    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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
     
    {
    "sports_content":{
    "sports_meta":{
    "date_time":"20121102 0136","season_meta":{
    "calendar_date":20121101,"season_year":2012,"stats_season_year":2012,"roster_season_year":2012,"schedule_season_year":2012,"standings_season_year":2012,"season_id":22012,"display_year":"2012-13","display_season":"Regular Season","season_stage":2}
    ,"next":{
    "url":""}
    }
    ,"article":{
    "pubDate":"11\/02\/12 01:35:54 ET","author":"By SANTOSH VENKATARAMAN, STATS Senior Writer","short":"Circumstances have changed for the Oklahoma City Thunder since they last played a meaningful home game.","title":"Trail Blazers-Thunder Preview","description":{
    "p":[{
    "text":"Circumstances have changed for the Oklahoma City Thunder since they last played a meaningful home game."}
    ,{
    "text":"The Thunder have won their last three home openers heading into Friday night's matchup with the Portland Trail Blazers."}
    ,{
    "text":"The last time Oklahoma City was preparing to play at home, it was Game 2 of the NBA Finals against Miami after the Thunder took the opener. The Heat, of course, won that contest and the next three at home to earn the title."}
    ,{
    "text":"The Thunder are still considered among the league's top contenders, but it's not clear yet how last week's trade of reigning Sixth Man of the Year James Harden to Houston will affect their chemistry. Kevin Martin, acquired in that deal, has filled Harden's role as Oklahoma City's top reserve."}
    ,{
    "text":"\"Even though they lost James Harden, Kevin Martin is a quality scorer off the bench,\" Portland coach Terry Stotts said."}
    ,{
    "text":"Oklahoma City opened its season Thursday with an 86-84 loss at San Antonio in a rematch of the Western Conference finals. Martin scored 15 points in his Thunder debut."}
    ,{
    "text":"\"I thought K-Mart did a great job,\" coach Scott Brooks said."}
    ,{
    "text":"Oklahoma City shot 37.7 percent in a game that featured 15 lead changes. Reigning scoring champion Kevin Durant had a game-high 23 points while Russell Westbrook finished with 18 points, eight rebounds, five assists and six turnovers."}
    ,{
    "text":"\"That was a great first game, it's the way to open up the season,\" Brooks said. \"I wish we had won the game but there's nothing to be ashamed of. I told the guys, 'Let's go back tomorrow night and play with the same type of force and intensity and toughness.'\""}
    ,{
    "text":"Westbrook will match up with Portland's Damian Lillard, the sixth overall pick in the draft. Lillard had 22 points and 11 assists in his NBA debut Wednesday in Portland's season-opening 116-106 victory over the Lakers."}
    ,{
    "text":"Lillard joined Hall of Famers Oscar Robertson and Isiah Thomas as the only players with at least 20 points and 10 assists in their debuts."}
    ,{
    "text":"Now he'll get his first look at the two-time All-Star Westbrook, who averaged 27.5 points, 7.3 assists and 8.8 boards as the Thunder took three of four from the Trail Blazers last season."}
    ,{
    "text":"\"He's going to be tough, he's a really explosive athlete, a really aggressive player looking to score the ball,\" said Lillard, who also had six turnovers Wednesday. \"It'll be a challenge just like every other night.\""}
    ,{
    "text":"Nicolas Batum had 26 points as all five starters scored in double figures and Wesley Matthews added 22 for Portland, which scored 28 points off 24 turnovers by the Lakers and now faces a Thunder team that was last in the league in that department with 16.3 per game in 2011-12."}
    ,{
    "text":"The Trail Blazers are beginning a three-game trip over four nights after going 8-25 on the road last season for the league's fourth-worst mark."}
    ,{
    "text":"\"I'm looking forward to it,\" Lillard said. \"In college you play one or two games a week so you got a lot of practice in between. At the end of the day, we all just wanna hoop, we wanna play.\""}
    ,{
    "text":"Durant's 23.8 career average against Portland is his second-worst against a Western Conference opponent."}
    ]}
    }
    }
    }
    Merci d'avance

    J'ai essayé pas mal de chose avec la fonction json_decode(#, true) ... mais je crois que j'ai loupé qqch

  2. #2
    Expert éminent
    Avatar de sekaijin
    Homme Profil pro
    Urbaniste
    Inscrit en
    Juillet 2004
    Messages
    4 205
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Urbaniste
    Secteur : Santé

    Informations forums :
    Inscription : Juillet 2004
    Messages : 4 205
    Points : 9 127
    Points
    9 127
    Par défaut
    c'est déjà dans un tableau ??
    et que vient faire json_decode dans javascript ? c'est du php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    var d = JSON.parse(jsonString);
    var paragraphs = d.sports_content.article.description.p;
    console.log(paragraphs[0]);
    console.log(paragraphs[1]);
    //...

    A+JYT

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    60
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 60
    Points : 42
    Points
    42
    Par défaut Merci beaucoup !!!
    Merci c'est niquel en javascript !!! et grâce à vous j'ai repéré la balise qui me manquait pour le faire également en php !!!! je mets le code également en dessous si quelqu'un cherchait à le faire en JS ou en PHP ...
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    $urlContent = json_decode(#, true);
    foreach($urlContent["sports_content"]["article"]["description"]["p"] as $game) {
        echo $game["text"].'<br>';
    }

    Encore merci pour tout !!!

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

Discussions similaires

  1. Parser un JSON avec des variables avec des "-" et des ":"
    Par Quentin33 dans le forum Windows Phone
    Réponses: 2
    Dernier message: 22/05/2011, 01h04
  2. Parser du json
    Par nox73 dans le forum Langage
    Réponses: 4
    Dernier message: 19/05/2011, 19h52
  3. Parser page JSON en PHP
    Par tonymx15 dans le forum Langage
    Réponses: 4
    Dernier message: 08/04/2011, 00h55
  4. [Prototype] Parser du JSON aprés une requête Ajax
    Par Gregory.M dans le forum Bibliothèques & Frameworks
    Réponses: 18
    Dernier message: 16/06/2010, 10h21
  5. parser un json
    Par Henry9 dans le forum jQuery
    Réponses: 4
    Dernier message: 18/05/2009, 21h13

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