用php解码json语法

时间:2014-04-03 09:16:25

标签: php json

我的json_decode有问题。我找不到正确的语法来检索以下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"
        },
      }
    ]
  }
}

现在,我有:

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

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

$titre = $aArticle->Titre;

}
你知道吗? 谢谢!

3 个答案:

答案 0 :(得分:1)

您的"结果"数据不是正确的json格式。

试试这种格式,它正在运作

  

{" name":" Emission"," count&#34 ;: 18," lastsuccess":" Thu Apr 03   2014 09:03:36 GMT + 0000(UTC)","结果":{" Bourdin   直接&#34 ;: [{"标题&#34 ;: {" HREF&#34 ;: [" HTTP://www.channel.com/video/"],& #34;文本":["该   电视   SHOW"]}"日期":[" 31/03/2014"]},{"标题" {" HREF&#34 ;:[" HTTP://www.channel.com/video/"]"文本&#34 ;: ["该   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"]; 
   }

答案 1 :(得分:0)

尝试使用var_dump()

Example

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

答案 2 :(得分:0)

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