Damien
Damien

Reputation: 333

decode json syntax with php

I have a problem with json_decode. I can not find the right syntax to retrieve the title in the following json :

 {
  "name": "Emission",
  "count": 18,
  "lastsuccess": "Thu Apr 03 2014 09:03:36 GMT+0000 (UTC)",
  "results": {
    "Bourdin Direct": [
      {
        "Titre": {
        "href": "http://www.channel.com/video/",
        "text": "The TV SHOW"
        },
        "Date": "31/03/2014",
        "Duree": {
        "href": "http://www.channel.com/187457/",
        "text": "19:06"
        },
      },
      {
        "Titre": {
        "href": "http://www.channel.com/video/theshow/",
        "text": "The title"
        },
        "Date": "28/03/2014",
        "Duree": {
        "href": "http://www.channel.com/video/28-03-186929/",
        "text": "19:42"
        },
      }
    ]
  }
}

For now, i have that :

$json = file_get_contents($url);
$data = json_decode($json);

foreach ($data->results as $aArticle) {

$titre = $aArticle->Titre;

}

Do you have any idea ? Thanks !

Upvotes: 0

Views: 50

Answers (3)

Smruti
Smruti

Reputation: 452

Your "results" data is not in proper json format.

Try this format, it's working

{ "name": "Emission", "count": 18, "lastsuccess": "Thu Apr 03 2014 09:03:36 GMT+0000 (UTC)", "results":{"Bourdin Direct":[{"Titre":{"href":["http://www.channel.com/video/"],"text":["The TV SHOW"]},"Date":["31/03/2014"]},{"Titre":{"href":["http://www.channel.com/video/"],"text":["The TV SHOW"]},"Date":["31/03/2014"]}]} }

   $json = file_get_contents($url);  
   $data = json_decode($json, true); 
   foreach ($data[results] as $k => $aArticle) {    
    $titre = $aArticle[0]['Titre']["href"]; 
   }

Upvotes: 1

blue
blue

Reputation: 1949

$titre = $aArticle->Titre->text;

Upvotes: 0

SagarPPanchal
SagarPPanchal

Reputation: 10121

Try using var_dump()

Example

var_dump(json_decode($json_array));
var_dump(json_decode($json_array, true));

Upvotes: 0

Related Questions