0

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 !

3
  • What's the wrong with your code? What is the result? Commented Apr 3, 2014 at 9:20
  • 1
    I dont think its a valid json.. u have some extra , jsonlint.com Commented Apr 3, 2014 at 9:20
  • Tip: once you decode it isn't JSON any more, it's a PHP object. print_r() the object to view its structure. Commented Apr 3, 2014 at 9:21

3 Answers 3

1

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"]; 
   }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot ! But it seems that my problem is not because of the Json format. I have edited it for this question, and maybe, i have made some mistakes. But even with the complete unedited Json, i still can't grab each titles. I have copy paste the complete json here : jsfiddle.net/yYF6H
yes, i got ur answer, Try this $data = json_decode($json, true); foreach ($data[results] as $k => $aArticle) { $titre = $aArticle[0]['Titre']["href"]; }
Thanks ! Now, the title is grab, but only for the first node. Do I have to add a i++ , for having $titre = $aArticle[1]['Titre']["text"];, and $titre = $aArticle[2]['Titre']["text"]; etc in a foreach ?
Yes u can add i++ or just add another foreach loop inside $data[results] as foreach ($data[results] as $k => $aArticle) { foreach($aArticle as $v){ print_r($v['Titre']["text"]); } }
0

Try using var_dump()

Example

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

Comments

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

1 Comment

Some prose would be nice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.