2013-07-05 73 views
-2

我想從this json文件中顯示以下值。
Titel的
datum_start
datum_end
我用這個代碼:用PHP解析JSON電視指南

<?php 
$nl1jsonurl='http://www.tvgids.nl/json/lists/programs.php?channels=1'; 
$nl1jsontext=file_get_contents($nl1jsonurl,true); 

$nl1 = json_decode($nl1jsontext); 
foreach($nl1->1 as $data) 
{ 
    echo $data->titel . "\n"; 
    echo $data->datum_start . "\n"; 
    echo $data->datum_end . "\n"; 

} 
?> 

樣品JSON:

{ 
    "1": [ 
     { 
      "db_id": "14612024", 
      "titel": "NOS Journaal", 
      "genre": "Nieuws/actualiteiten", 
      "soort": "", 
      "kijkwijzer": "", 
      "artikel_id": null, 
      "artikel_titel": null, 
      "datum_start": "2013-07-05 00:10:00", 
      "datum_end": "2013-07-05 00:35:00" 
     }, 
     { 
      "db_id": "14642893", 
      "titel": "NOS Studio Sportzomer: De Avondetappe", 
      "genre": "Sport", 
      "soort": "", 
      "kijkwijzer": "", 
      "artikel_id": "58162", 
      "artikel_titel": "Sprinten in Montpellier", 
      "datum_start": "2013-07-05 00:35:00", 
      "datum_end": "2013-07-05 01:30:00" 
     } 
    ] 
} 
+1

你質疑* iiiiis *? –

+1

這裏有一個問號,就在第2行。問題是「php」。答案是肯定的。 – neokio

+0

可能會給你stClass不能轉換爲字符串的錯誤,試試這個'$ nl1 = json_decode($ nl1jsontext,true);' – dakshbhatt21

回答

-2

這可能有助於你

<?php 
$nl1jsonurl='http://www.tvgids.nl/json/lists/programs.php?channels=1'; 
$nl1jsontext=file_get_contents($nl1jsonurl,true); 

//decode the content lke this 
$nl1 = json_decode($nl1jsontext, TRUE); 

//print_r($nl1); 

foreach($nl1['1'] as $data) 
{ 
    echo $data['title'] . "\n"; 
    echo $data['datum_start'] . "\n"; 
    echo $data['datum_end'] . "\n"; 

} 
1

因爲你的JSON有一個名爲1屬性,你必須訪問它像這樣:

foreach($nl1->{'1'} as $data) 

如果您嘗試使用$nl1->1,PHP將產生語法錯誤。