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 confirm�
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    60
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 60
    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 confirm�
    Avatar de sekaijin
    Homme Profil pro
    Urbaniste
    Inscrit en
    Juillet 2004
    Messages
    4 205
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 61
    Localisation : France, Yvelines (�le de France)

    Informations professionnelles :
    Activit� : Urbaniste
    Secteur : Sant�

    Informations forums :
    Inscription : Juillet 2004
    Messages : 4 205
    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 confirm�
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    60
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 60
    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